How would you determine if a given string is contained in that wordlist?
set string = 'c'
or: set string = 'ico'
I have tried the following:
if ( "$test" =~ *$string* ) then ... (returns true for both cases)
if ( "$test" =~ [ ]*$string*[ ] ) ... ( [ confuses the shell)
if ( "$test" =~ '[ ]'*$string*[ ]' ) (fails too)
My csh book says quotes can't be used with =~. I was hoping there was
something more elegant than switching to a foreach loop. Any ideas ? Thanks.
Given a space delimited wordlist:
set wordlist = ' ico dlg res '
How would you determine if a given string is contained in that wordlist?
set string = 'c'
or: set string = 'ico'
The following works fine:
if ( "$wordlist" =~ *' '$string' '* ) then ...
But that requires leading and trailing spaces in the wordlist (see
given). I suspect that if somebody changes the script to add items to
the wordlist that they may accidentally remove those spaces. Is there a
better, simple way to do this?
|> Given a space delimited wordlist:
|> set wordlist = ' ico dlg res '
|> How would you determine if a given string is contained in that wordlist?
|> set string = 'c'
|> or: set string = 'ico'
|> The following works fine:
|> if ( "$wordlist" =~ *' '$string' '* ) then ...
|> But that requires leading and trailing spaces in the wordlist (see
|> given). I suspect that if somebody changes the script to add items to
|> the wordlist that they may accidentally remove those spaces. Is there a
|> better, simple way to do this?
How about this, not much simpler but avoids having to put spaces into
$wordlist:
if ( " $wordlist " =~ *" $string "* ) then ...
--
Andreas Schwab "And now for something
sch...@issan.informatik.uni-dortmund.de completely different"
I believe that the 'expr' command will help you. The expr command
has many features, but the one shown below returns the number of
characters matched.
% set wordlist = (word1 word2 word3)
% expr "$wordlist" : word1
5
% expr "$wordlist" : foobar
0
The expression on the right of the colon can be a regular expression.
NOTE: the quotes around $wordlist are needed to convert the wordlist
into a string.
Depending on your version of UNIX, the expr has some other nice
features for handling string functions:
o substr
o index
o length