Solved

How to check if value in a list in Perl?

Posted on 2008-10-21
7
3,050 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
[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
7 Comments
 
LVL 10

Expert Comment

by:Tobias
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
Industry Leaders: 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 10

Expert Comment

by:Tobias
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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Six Sigma Control Plans

688 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