Solved

Is there a Korn shell data type equivlent to Pascal sets?

Posted on 2013-01-09
6
281 Views
Last Modified: 2013-01-18
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...
0
Comment
Question by:babyb00mer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38760309
How about?

set="1 5 9 21 34"
if $(echo $set | grep -q "\b$NUMBER\b")
then
     echo do something
fi

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 38760365
SET='^(1|5|9|21|34)$'
[[ $NUMBER =~ $SET ]] && Do something

SET=" 1 5 9 21 34 "
[[ $SET =~ " $NUMBER " ]] && Do something
0
 

Author Comment

by:babyb00mer
ID: 38760586
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 38760615
Does your version of ksh support this?

SET=" 1 5 9 21 34 "   
[[ ${SET/ $NUMBER } != $SET ]] && Do something
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38760734
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" ...
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 38760856
SET=" 1 5 9 21 34 "
[[ "$SET" == *" $NUMBER "*  ]]
 will not match with NUMBER="3"
nor will
SET="1|5|9|21|34"
[[ $NUMBER == $SET  ]]
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question