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.
UPDATE: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 (Monsignor) on Jun 25, 2013 at 22:03 UTC | |
by Laurent_R (Canon) on Jun 26, 2013 at 21:02 UTC | |
by Eily (Monsignor) on Jun 26, 2013 at 22:06 UTC | |
by Laurent_R (Canon) on Jun 28, 2013 at 18:52 UTC |