in reply to Re: Avoiding repeated undefs
in thread [Solved] Avoiding repeated undefs
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Avoiding repeated undefs
by Eily (Monsignor) on Feb 26, 2019 at 14:57 UTC | |
by rsFalse (Chaplain) on Feb 27, 2019 at 22:41 UTC | |
by choroba (Cardinal) on Feb 28, 2019 at 06:42 UTC | |
|
Re^3: Avoiding repeated undefs
by parv (Parson) on Feb 26, 2019 at 14:17 UTC |