Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: regex question

by AnomalousMonk (Bishop)
on Apr 02, 2020 at 01:45 UTC ( #11114938=note: print w/replies, xml ) Need Help??


in reply to regex question

morgon:   Further to Corion's post:

[Corion]: The match runs to completion before the map even starts.

At the time your map runs, $1 and $2 are what they are at the end of the global match.
Here's an instrumented version of the OPed code to better (I hope) show what's happening:
c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dumper; ;; my $s = \"1 A\n2 B\n33 foo\n\"; ;; my %h = map { print qq{in map: \$_ '$_' \$1 '$1' \$2 '$2'}; $1 => $2; } $s =~ /(\d+)\s+(\S+)/mg ; print Dumper(\%h); " in map: $_ '1' $1 '33' $2 'foo' in map: $_ 'A' $1 '33' $2 'foo' in map: $_ '2' $1 '33' $2 'foo' in map: $_ 'B' $1 '33' $2 'foo' in map: $_ '33' $1 '33' $2 'foo' in map: $_ 'foo' $1 '33' $2 'foo' $VAR1 = { '33' => 'foo' };
(Note that this example doesn't take into account the revised data format specification suggested here. :)


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2020-06-05 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?