in reply to Re^2: Regex to array
in thread Regex to array

... where it's documented ...

See List value constructors in perldata:

... you may assign to "undef" in a list. This is useful for throwing away some of the return values of a function ...
Assignment to undef works for any list assignment, but if you're assigning from an array or list, it's usually more sane to use some kind of slice rather than fooling around with undef.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Regex to array
by Eily (Monsignor) on Jan 09, 2018 at 17:51 UTC

    See List value constructors in perldata
    perldata! Thanks!

    but if you're assigning from an array or list, it's usually more sane to use some kind of slice rather than fooling around with undef.
    Why? To make a slice in this case you'd need to know in advance how many matches there will be, or first store the output in an array and then slice away the first element. It's doable and understandable, but I don't see how skipping a value with undef is less sane.

      Why? To make a slice in this case ...

      The particular case I had in mind was the
          (undef, my %section) = split /(SECTION \d+)/, $text;
      statement dealing with return values from the split built-in function, and I agree that in this kind of case, assignment to undef is reasonable and IMHO preferable.

      However, I was trying to make the additional point that this kind of assignment works with any array or list assignment, e.g.,

      c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = qw(zero one two three four five); ;; my (undef, undef, $x, $y) = @ra; print qq{'$x' '$y'}; " 'two' 'three'
      and that in this sort of case it's (usually) better to use some kind of slice, e.g.,
          my ($x, $y) = @ra[ 2, 3 ];


      Give a man a fish:  <%-{-{-{-<

        Oh alright then, it's mostly a style issue right? I agree that when you can use a slice on an array it is better (at least because it's more common so less confusing). I personally don't like slices on lists (ie: aritrary expressions in parentheses); most of the time I just use a temp array that I can slice from but otherwise I'd rather go with the undef version.