Edgar Cole
asked on
Is there a Korn shell data type equivlent to Pascal sets?
I know! It's just wishful thinking. Just thought I'd put it out there.
I have a situation for which, if I could use Pascal, I'd define a set type and declare a set variable which I could reference in a construct similar to the following...
SOMETHING = (me, myself, I)
SET = set of SOMETHING
THIS: SET
THAT: SET
readln(THIS)
if THIS in THAT; then...
My Pascal is a little rusty, but...
What I'm trying to do is determine whether one number is included in a set of other numbers. So far, all I've come up with is the case statement:
case $NUMBER in
1|5|9|21|34) Do something or do nothing, as it were...
I have a situation for which, if I could use Pascal, I'd define a set type and declare a set variable which I could reference in a construct similar to the following...
SOMETHING = (me, myself, I)
SET = set of SOMETHING
THIS: SET
THAT: SET
readln(THIS)
if THIS in THAT; then...
My Pascal is a little rusty, but...
What I'm trying to do is determine whether one number is included in a set of other numbers. So far, all I've come up with is the case statement:
case $NUMBER in
1|5|9|21|34) Do something or do nothing, as it were...
SET='^(1|5|9|21|34)$'
[[ $NUMBER =~ $SET ]] && Do something
SET=" 1 5 9 21 34 "
[[ $SET =~ " $NUMBER " ]] && Do something
[[ $NUMBER =~ $SET ]] && Do something
SET=" 1 5 9 21 34 "
[[ $SET =~ " $NUMBER " ]] && Do something
ASKER
Hmm. I've never seen the '=~' operator. Unfortunately, it doesn't look like my versions of the Korn shell will accept it. I apologize. I forgot to mention that I'm running on AIX 5.3 and 6.1.
Does your version of ksh support this?
SET=" 1 5 9 21 34 "
[[ ${SET/ $NUMBER } != $SET ]] && Do something
SET=" 1 5 9 21 34 "
[[ ${SET/ $NUMBER } != $SET ]] && Do something
SET=" 1 5 9 21 34 "
NUMBER=34
if [[ $(expr index "$SET" "$NUMBER") -ne 0 ]] then
echo Found. Do something.
else
echo Not found. Do nothing.
fi
or
SET=" 1 5 9 21 34 "
NUMBER="34"
if [[ "$SET" == *"$NUMBER"* ]] then
echo Found. Do something.
else
echo Not found. Do nothing.
fi
Unfortunately, the above will also match with e. g. NUMBER="3" (but at least the second one will not match with NUMBER=" 3 "), so I think farzanj's solution is best (so far), but without "\b", because our grep doesn't understand this syntax. Use "-w" (word match) instead:
... grep -wq "$NUMBER" ...
NUMBER=34
if [[ $(expr index "$SET" "$NUMBER") -ne 0 ]] then
echo Found. Do something.
else
echo Not found. Do nothing.
fi
or
SET=" 1 5 9 21 34 "
NUMBER="34"
if [[ "$SET" == *"$NUMBER"* ]] then
echo Found. Do something.
else
echo Not found. Do nothing.
fi
Unfortunately, the above will also match with e. g. NUMBER="3" (but at least the second one will not match with NUMBER=" 3 "), so I think farzanj's solution is best (so far), but without "\b", because our grep doesn't understand this syntax. Use "-w" (word match) instead:
... grep -wq "$NUMBER" ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window