?
Solved

How to check if value in a list in Perl?

Posted on 2008-10-21
7
Medium Priority
?
3,053 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
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 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

770 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