Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Testing Bash arrays for a string

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
overflow
LXF regular


Joined: Tue Aug 23, 2005 2:40 pm
Posts: 158
Location: London

PostPosted: Fri Jun 02, 2006 3:55 pm    Post subject: Testing Bash arrays for a string Reply with quote

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
View user's profile Send private message
Steogede
LXF regular


Joined: Thu May 04, 2006 6:39 pm
Posts: 146

PostPosted: Mon Jun 19, 2006 11:39 am    Post subject: RE: Testing Bash arrays for a string Reply with quote

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
View user's profile Send private message
overflow
LXF regular


Joined: Tue Aug 23, 2005 2:40 pm
Posts: 158
Location: London

PostPosted: Mon Jun 19, 2006 11:51 am    Post subject: RE: Testing Bash arrays for a string Reply with quote

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
View user's profile Send private message
Steogede
LXF regular


Joined: Thu May 04, 2006 6:39 pm
Posts: 146

PostPosted: Mon Jun 19, 2006 5:08 pm    Post subject: Re: RE: Testing Bash arrays for a string Reply with quote

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 user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
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
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast