Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^2: Test if multiple variables are defined

by Eily (Parson)
on Jun 25, 2013 at 22:03 UTC ( #1040664=note: print w/replies, xml ) Need Help??

in reply to Re: Test if multiple variables are defined
in thread Test if multiple variables are defined

What about a simple grep like this?

s/^\s+|\s+$//g grep {defined $_} for ( $ipaddress, $prefix, $interface, $device, $locati;

That wouldn't work, s/// either works on the string binded to it with =~ (or !~) or on $_ otherwise, not on a list parameter. Look at AnomalousMonk's answer for the correct syntax.

  • Comment on Re^2: Test if multiple variables are defined

Replies are listed 'Best First'.
Re^3: Test if multiple variables are defined
by Laurent_R (Canon) on Jun 26, 2013 at 21:02 UTC

    Yes, you are right and I definitely know it. As noted in my update above, I really meant to write:

    s/^\s+|\s+$//g for grep {defined $_} ( $ipaddress, $prefix, $interface, $device, $location);

    But, as I also said in my update, that does not work as I expected, the "for" does not seem to deliver to the s/// statement the elements supplied by the grep. I got it working quite easily using a map instead of a for, but it no longer has the relative cleverness I was looking for.

      use strict; use warnings; use Data::Dumper; my ( $ipaddress, $prefix, $interface, $device, $location) = ('no_ws', +' some_ws ', undef, ' lotsa ws ', undef); s/^\s+|\s+$//g for grep {defined $_} ( $ipaddress, $prefix, $interface +, $device, $location); print Dumper ( $ipaddress, $prefix, $interface, $device, $location);
      (Thanks AnomalousMonk for the strings :D). So this yields:
      $VAR1 = 'no_ws'; $VAR2 = 'some_ws'; $VAR3 = undef; $VAR4 = 'lotsa ws'; $VAR5 = undef;
      It works fine.

      Alright, I must have done something else wrong in my test. Thank you for your message.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040664]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-01-19 01:43 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.