Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

csh wordlist operations

46 views
Skip to first unread message

Chris Sites - Integrated Industrial Info.

unread,
Feb 28, 1995, 11:53:57 AM2/28/95
to

Chris Sites - Integrated Industrial Info.

unread,
Feb 28, 1995, 11:56:53 AM2/28/95
to
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'

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.


Chris Sites - Integrated Industrial Info.

unread,
Feb 28, 1995, 10:47:43 PM2/28/95
to
Well, I'm sorry that was a brain dead post. Let me ask a better question
along the same lines but with my brain in gear:

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?

Andreas Schwab

unread,
Mar 1, 1995, 8:20:09 AM3/1/95
to
In article <3j0qov$r...@redstone.interpath.net>, i...@mercury.interpath.net (Chris Sites - Integrated Industrial Info.) writes:

|> 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"

Jeffrey M. Altbush

unread,
Mar 8, 1995, 12:03:10 PM3/8/95
to

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

0 new messages