<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Wed May 22, 2013 6:16 pm by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Wed May 22, 2013 6:16 pm</pubDate>
  <lastBuildDate>Wed May 22, 2013 6:16 pm</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: RE: Testing Bash arrays for a string</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=25800#25800</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=7448'&gt;Steogede&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Jun 19, 2006 5:08 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;overflow wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;Well, how about Python:&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;myset=&amp;#40;'oak', 'pine', 'beach', 'elm'&amp;#41;&lt;br /&gt;
if tree in myset&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; ...do something&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
It doesn't get much tidier than that.&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
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) ...  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Quote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;I'm not sure I get your point about verbosity. High level languages more verbose than assembly language? How's that?&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
Sorry, your right, that makes no sense.  My mind tends to wander of on its own sometimes.</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=25800#25800</comments>
                                        <author>Steogede</author>
                                        <pubDate>Mon Jun 19, 2006 5:08 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=25800#25800</guid>
                                      </item>
                                      <item>
                                        <title>RE: Testing Bash arrays for a string</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=25787#25787</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=2827'&gt;overflow&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Jun 19, 2006 11:51 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Well, how about Python:&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;myset=&amp;#40;'oak', 'pine', 'beach', 'elm'&amp;#41;&lt;br /&gt;
if tree in myset&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; ...do something&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
It doesn't get much tidier than that.&lt;br /&gt;
&lt;br /&gt;
I'm not sure I get your point about verbosity. High level languages more verbose than assembly language? How's that?</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=25787#25787</comments>
                                        <author>overflow</author>
                                        <pubDate>Mon Jun 19, 2006 11:51 am</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=25787#25787</guid>
                                      </item>
                                      <item>
                                        <title>RE: Testing Bash arrays for a string</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=25786#25786</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=7448'&gt;Steogede&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Jun 19, 2006 11:39 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      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.&lt;br /&gt;
&lt;br /&gt;
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 ]] &amp;amp;&amp;amp; whatever. e.g:&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
myset=&amp;#40;oak pine beach elm&amp;#41;&lt;br /&gt;
seaside=&amp;quot;beach&amp;quot;&lt;br /&gt;
&amp;#91;&amp;#91; $&amp;#123;myset&amp;#91;*&amp;#93;&amp;#125; =~ \\b$&amp;#123;seaside&amp;#125;\\b &amp;#93;&amp;#93; &amp;amp;&amp;amp; echo &amp;quot;$&amp;#123;myset&amp;#91;*&amp;#93;&amp;#125; contains $seaside&amp;quot;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
What were you hoping for in terms of elegance?  Do you have an example from another programming language?</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=25786#25786</comments>
                                        <author>Steogede</author>
                                        <pubDate>Mon Jun 19, 2006 11:39 am</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=25786#25786</guid>
                                      </item>
                                      <item>
                                        <title>Testing Bash arrays for a string</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=24745#24745</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=2827'&gt;overflow&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Jun 02, 2006 3:55 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      I'm trying to test a Bash array for any element containing a string. A whole element and the whole string - no substrings.&lt;br /&gt;
&lt;br /&gt;
Long-hand, I could do it like this:&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;myset=&amp;#40;oak pine beach elm&amp;#41;&lt;br /&gt;
for i in $&amp;#123;myset&amp;#91;@&amp;#93;&amp;#125;; do if &amp;#91;&amp;#91; $i == $&amp;#123;tree&amp;#125; &amp;#93;&amp;#93; ; then ... ; fi; done&lt;br /&gt;
&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
which seems a bit long-winded. &lt;br /&gt;
&lt;br /&gt;
Or I could almost do it like this:&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;myset=&amp;#40;oak pine beach elm&amp;#41;&lt;br /&gt;
if &amp;#91;&amp;#91; $&amp;#123;myset&amp;#91;*&amp;#93;&amp;#125; =~ $&amp;#123;tree&amp;#125; &amp;#93;&amp;#93; ; then ... ; fi&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
Which is tidier but suffers from a drawback: it will match substrings. It matches 'pin', for example.&lt;br /&gt;
&lt;br /&gt;
I can force word boundaries to overcome the substring problem, like this:&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;myset=&amp;#40;oak pine beach elm&amp;#41;&lt;br /&gt;
if &amp;#91;&amp;#91; $&amp;#123;myset&amp;#91;*&amp;#93;&amp;#125; =~ \\b$&amp;#123;tree&amp;#125;\\b &amp;#93;&amp;#93; ; then ... ; fi&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
but it still isn't very elegant. &lt;br /&gt;
&lt;br /&gt;
Anyone got a better approach?&lt;br /&gt;
&lt;br /&gt;
Thanks</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=24745#24745</comments>
                                        <author>overflow</author>
                                        <pubDate>Fri Jun 02, 2006 3:55 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=24745#24745</guid>
                                      </item></channel></rss>