Testing Bash arrays for a string

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Testing Bash arrays for a string

Postby overflow » Fri Jun 02, 2006 2:55 pm

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: Select all
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: Select all
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: Select all
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
User avatar
overflow
LXF regular
 
Posts: 158
Joined: Tue Aug 23, 2005 1:40 pm
Location: London

RE: Testing Bash arrays for a string

Postby Steogede » Mon Jun 19, 2006 10:39 am

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: Select all
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?
Steogede
LXF regular
 
Posts: 146
Joined: Thu May 04, 2006 5:39 pm

RE: Testing Bash arrays for a string

Postby overflow » Mon Jun 19, 2006 10:51 am

Well, how about Python:
Code: Select all
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
User avatar
overflow
LXF regular
 
Posts: 158
Joined: Tue Aug 23, 2005 1:40 pm
Location: London

Re: RE: Testing Bash arrays for a string

Postby Steogede » Mon Jun 19, 2006 4:08 pm

overflow wrote:Well, how about Python:
Code: Select all
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) ...




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.
Steogede
LXF regular
 
Posts: 146
Joined: Thu May 04, 2006 5:39 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests