Trying the first one, which I thought extremely elegant, I ran into some unexpected behaviour.
dr@dns:~$ cat sscce.pl use strict; use warnings; use feature 'say'; my $str = 'a b c d e f '; my ($key, undef, undef, undef, undef, undef, $val) = split(" ", $str); say "Val = <$val>"; my ($key2, $val2) = (split(" ", $str))[0,6]; say "Val2 = <$val2>"; dr@dns:~$ perl sscce.pl Val = <> Use of uninitialized value $val2 in concatenation (.) or string at ssc +ce.pl line 9. Val2 = <>
Some of the data I am hacking have only six data points, but with a trailing space. In my original version I was getting a zero length string as the last value as shown in the SSCCE above. But using the more elegant version, it becomes undef and I get the warning shown. Actually, I find it more logical to get undef and changing my code would be no problem, but I can't find any indication why split should behave so apparently inconsistently. I have tried using the regex in my OP, but get the same results as in the SSCCE.
As I said in my OP, I have working code. This isn't important, but it is a gap in my knowledge that I'd like to close.
Regards,
John Davies
In reply to Re^2: Avoiding repeated undefs
by davies
in thread [Solved] Avoiding repeated undefs
by davies
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |