Solved

How to check if value in a list in Perl?

Posted on 2008-10-21
7
3,043 Views
Last Modified: 2012-05-05
Hi,

I am trying to create a conditional statement "if" that will check if a value is in a list of values. Right now, I simply do this:

if ( $myvar == 'VAR0123' || $myvar == 'VAR1234' || $myvar == 'VAR2345' )
{
}

I would prefer to setup all my "values" in an array or hash at one place and then do something like:

if ( $myvar ISIN $mylist )
{
}

Can this be done easily in Perl? Maybe using a hash table? Any ideas?
0
Comment
Question by:bganoush
7 Comments
 
LVL 10

Expert Comment

by:MadShiva
ID: 22766921
Hi !


Maybe there is one better solution, but is one solution :


#!/usr/bin/perl

@list = (VAR0123,VAR1234,VAR2345);

foreach $i (@list)
{

          if ( $i == 'VAR0123')
          {

          }

}
0
 

Author Comment

by:bganoush
ID: 22767002

Nevermind.. I think I know what to do...

If I create a hash like this...
my %mylist = ('VAR0123' => 1, 'VAR1234' => 1, 'VAR2345' => 1);

then I simply check if the hash returns a value like this:

if ( $mylist{$myvar} )
{
}

Would that work?  Also, is it possible to do this if the hash names are only numeric?  example: instead of 'VAR1234', I have '000001234'....
0
 

Author Comment

by:bganoush
ID: 22767023

I wonder also...

What if I put all my values in a text array like this:

$mylist = '000000123 000001234 000002345';

I guess I can now use a regex to "find" the value?

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 10

Expert Comment

by:MadShiva
ID: 22767241
Hi !


$mylist = '000000123 000001234 000002345';

@list = split(/ /, $mylist);

foreach $i (@list)
{

if($i=~ m/$myvar/)
            {
            print " is in ";
            }

}



OR



$mylist = '000000123 000001234 000002345';


if($mylist =~ m/$myvar/)
            {
            print " is in ";
            }


Best Regards
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22768507
A hash will work if what you have is a number.  The key is always interpreted as a string though, so if you have a number, it will be interpreted as a string.

If you have a single string, and want to use a regex, you can do this.  Note that because this is processed as a string, the number '00123' is different from '0123' which is different from '123.0'.
$myvar = '000000123';
$mylist = '(000000123|000001234|000002345)';
if($myvar =~ /$mylist/) {
    print "myvar is in mylist\n";
}
else {
    print "myvar is NOT in mylist\n";
}

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 22768951
generally, $mylist{$myvar} would be better for this kind of thing than grep or foreach or =~
$i == 'VAR0123'
will match anything that does not look like a number
0
 

Author Closing Comment

by:bganoush
ID: 31508242
Thanks.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

789 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