Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Test if multiple variables are defined

by Laurent_R (Abbot)
on Jun 25, 2013 at 21:50 UTC ( #1040663=note: print w/replies, xml ) Need Help??

in reply to 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;

The syntax above might not be perfect, there may be a comma or something else needed, but it conveys the idea, I think.


I typed the above without having a chance to test and I stupidly misplaced my "for". I really meant to write:

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

That probably makes much more sense (the "for" is totally useless between the grep block and the list, but, as placed now, it should give the grep results one by one aliased to $_ to the s/// statement), but that still does not work properly (for some reason, I have trouble using for and foreach with map or grep, it often does not really work the way I expect). So, having now a chance to test, I can change it somewhat to get it working the way I wanted:

my @result = map {s/^\s+|\s+$//g; $_}  grep {defined $_} ( $ipaddress, $prefix, $interface, $device, $location);

or simply

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

However, using map in this context is probably not be the best, because it is not completely obvious to the eye that the elements of the original list are modified by the command.

Replies are listed 'Best First'.
Re^2: Test if multiple variables are defined
by Eily (Vicar) on Jun 25, 2013 at 22:03 UTC

    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.

      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://1040663]
[1nickt]: Yaerox probably best to whip up an SSCCE demonstrating the failure in what you have now and post to SoPW as a question.
[Corion]: Yaerox: That's a somewhat hard problem. Encode solves the conversion part, but for guessing what encoding a file is in, that's the hard part
[Corion]: Yaerox: There is Encode::Guess, but that needs a limited set of inputs, and it also cannot handle multiple single-byte encodings
[Corion]: If you have a BOM, that's a really easy way to recognize UTF-8. Otherwise, you can try to decode a file from UTF-8, and if that works OK and doesn't crash, most likely the file was valid UTF-8
[Corion]: But as "ansi" (Latin-1?) is a single-byte encoding, any file is a valid ANSI file

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-03-28 13:13 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (332 votes). Check out past polls.