Solved

How to check if value in a list in Perl?

Posted on 2008-10-21
7
3,038 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to extract data from a table into a fixed length file 10 54
transpose into pipe delemited 8 67
perl to convert excel to csv 3 207
problem with using the glob function 1 69
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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