MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:
As I understand it, that will set to the empty string and trim any undefined strings that are split. However, that is dependent on how many pipe symbols there are in str. So, it is very possible that $var2 through $var5 will still be undefined after this code is executed. What is a better way of writing this code so that I'm sure all the variables in the array are at least defined or set to the empty string, even if there aren't enough pipe symbols in $str? Also, am I correct in thinking that the $_ ||= "" in the map is redundant since the parts of the string that are split will always be defined?my ($var1, $var2, $var3, $var4, $var5) = map { $_ ||= ""; Trim($_); $_ +; } split(/\|/, $str); sub Trim { $_[0] =~ /\s+$//g; $_[0] =~ /^\s+//g; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to clean an array of strings after a split
by kcott (Archbishop) on Aug 29, 2013 at 11:36 UTC | |
|
Re: How to clean an array of strings after a split
by choroba (Cardinal) on Aug 29, 2013 at 11:17 UTC | |
|
Re: How to clean an array of strings after a split
by AnomalousMonk (Archbishop) on Aug 29, 2013 at 13:45 UTC | |
by MrSnrub (Beadle) on Aug 29, 2013 at 15:37 UTC | |
by AnomalousMonk (Archbishop) on Aug 29, 2013 at 20:26 UTC | |
|
Re: How to clean an array of strings after a split
by hdb (Monsignor) on Aug 29, 2013 at 12:40 UTC | |
by MrSnrub (Beadle) on Aug 29, 2013 at 14:21 UTC | |
by hdb (Monsignor) on Aug 29, 2013 at 14:37 UTC |