Discussion Locked

Perl Lists - if $test in [ x, y, z] then

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I remember doing something like the above in C, but cannot find the answer while using perl.

I would like to take an assigned variable, and compare it to a list:

If $s_port in [80, 8080, 9999] then $www = True.

Anyhelp would be appreciated.
Thanks,
David Sarvai

5 replies

by jonasbn

Hello David,

There are two solutions:

1. You iterate over your list :-/
2. you regard it as strings and make a regex.

jonasbn

by jonasbn

foreach (qw(80 8080 9999)) { $www = 'true' if ($s_port == $_) }

To illustrate, I cannot come up with other ways to do it in Perl.

by dsarvai

Jonasbn,
Thanks for the illustration... That will give me something to work with!

Dave

This discussion is closed to new replies.