 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
overflow LXF regular

Joined: Tue Aug 23, 2005 2:40 pm Posts: 158 Location: London
|
Posted: Fri Jun 02, 2006 3:55 pm Post subject: Testing Bash arrays for a string |
|
|
I'm trying to test a Bash array for any element containing a string. A whole element and the whole string - no substrings.
Long-hand, I could do it like this:
| Code: | myset=(oak pine beach elm)
for i in ${myset[@]}; do if [[ $i == ${tree} ]] ; then ... ; fi; done
|
which seems a bit long-winded.
Or I could almost do it like this:
| Code: | myset=(oak pine beach elm)
if [[ ${myset[*]} =~ ${tree} ]] ; then ... ; fi
|
Which is tidier but suffers from a drawback: it will match substrings. It matches 'pin', for example.
I can force word boundaries to overcome the substring problem, like this:
| Code: | myset=(oak pine beach elm)
if [[ ${myset[*]} =~ \\b${tree}\\b ]] ; then ... ; fi
|
but it still isn't very elegant.
Anyone got a better approach?
Thanks _________________ overflow |
|
| Back to top |
|
 |
Steogede LXF regular
Joined: Thu May 04, 2006 6:39 pm Posts: 146
|
Posted: Mon Jun 19, 2006 11:39 am Post subject: RE: Testing Bash arrays for a string |
|
|
My honest opinion, the first option seems fine. Okay it is a bit verbose, but verbosity is often a useful thing - indeed, it is one of the main reason we use high level programming languages rather than doing everything in assembly. The only thing which seems to be missing from option 1 is an exit statement to take you out of the for loop once you have found a match.
As for elegance, I think that is a matter of taste, your first solution seems perfectly elegant. I don't really see how it could be more elegant. You could make it more efficient perhaps, option 3 has that covered. The only thing you could add to (or take away from) option 3 is to make it more terse. If you wanted something more terse you could use [[ test ]] && whatever. e.g: | Code: |
myset=(oak pine beach elm)
seaside="beach"
[[ ${myset[*]} =~ \\b${seaside}\\b ]] && echo "${myset[*]} contains $seaside" |
What were you hoping for in terms of elegance? Do you have an example from another programming language? |
|
| Back to top |
|
 |
overflow LXF regular

Joined: Tue Aug 23, 2005 2:40 pm Posts: 158 Location: London
|
Posted: Mon Jun 19, 2006 11:51 am Post subject: RE: Testing Bash arrays for a string |
|
|
Well, how about Python: | Code: | myset=('oak', 'pine', 'beach', 'elm')
if tree in myset:
...do something
|
It doesn't get much tidier than that.
I'm not sure I get your point about verbosity. High level languages more verbose than assembly language? How's that? _________________ overflow |
|
| Back to top |
|
 |
Steogede LXF regular
Joined: Thu May 04, 2006 6:39 pm Posts: 146
|
Posted: Mon Jun 19, 2006 5:08 pm Post subject: Re: RE: Testing Bash arrays for a string |
|
|
| overflow wrote: | Well, how about Python: | Code: | myset=('oak', 'pine', 'beach', 'elm')
if tree in myset:
...do something
|
It doesn't get much tidier than that. |
I don't script in bash a great deal, but I would be surprised if there was a neater option than you have already posted. You could create a function to check if the element exists, then use that in an if statement e.g. if exists(elem,arr) ...
| Quote: | | I'm not sure I get your point about verbosity. High level languages more verbose than assembly language? How's that? |
Sorry, your right, that makes no sense. My mind tends to wander of on its own sometimes. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|