Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: regex question

by AnomalousMonk (Archbishop)
on Apr 02, 2020 at 01:45 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114938]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found