Solved

How to check if value in a list in Perl?

Posted on 2008-10-21
7
3,023 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now