Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Regex to array

by Eily (Monsignor)
on Jan 09, 2018 at 13:43 UTC ( [id://1206976]=note: print w/replies, xml ) Need Help??


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

Or there's the option of using undef to ignore a value in a list. I don't remember where it's documented though: (undef, my %section) = split /(SECTION \d+)/, $text;

Replies are listed 'Best First'.
Re^3: Regex to array
by AnomalousMonk (Archbishop) on Jan 09, 2018 at 17:28 UTC
    ... 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:  <%-{-{-{-<

      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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-20 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found