Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Regex to array

by choroba (Cardinal)
on Jan 09, 2018 at 12:46 UTC ( [id://1206970]=note: print w/replies, xml ) Need Help??


in reply to Regex to array

The only problem is the empty field created by split at the start of the text, otherwise you'd have been able to assign the result directly to %sections. But you can create a false entry at the beginning and remove it later:
#! /usr/bin/perl use warnings; use strict; my $text = << '__TEXT__'; SECTION 1 This is the first section. SECTION 2 And this is the second one. __TEXT__ my %section = ("", split /(SECTION \d+)/, $text); delete $section{""}; use Data::Dumper; print Dumper \%section;

Update: Note that hashes use curly brackets, not the square ones (unlike in PHP).

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

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

    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;

      ... 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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found