perlguy has asked for the wisdom of the Perl Monks concerning the following question:
Wise monks:
While working on a quick script to parse some files for our site, I came across something peculiar.
In the code below, I'm setting my hash %date to '' in each of the three elements: YEAR, MONTH, and DATE. I then use embedded code in a regular expression to populate those fields.
On the first pass, everything comes out as expected, i.e. %date is populated correctly. But on every subsequent pass, %date is populated inside of the regular expression (as demonstrated), but upon leaving the regular expression, it seems to clear itself.
When the @file_names are rearranged, there is no different effect. It still works on the first iteration, and the remaining two do not. Any thoughts? I'm sure I'm overlooking something.
use strict; use warnings; use Data::Dumper; my @file_names = qw( /a/990101ag/toc.html /a/20000115/toc.html /a/990115ag/toc.html ); for my $file_name ( @file_names ) { print "Now working on $file_name\n"; my %date = ( YEAR => '', MONTH => '', DAY => '' ); $file_name =~ m# (?<=/a/) (\d{2,4})(\d{2})(\d{2})(?:ag)? (?=/) (?{ @date{'YEAR', 'MONTH', 'DAY'} = ( ( length($1) == 2 ? '19' . $1 : $1), $2, $3 ) }) (?{ print "Here, \%date is populated (joined): ", join('-', @date{'YEAR', 'MONTH', 'DAY'}), "\n" }) #x; print "But after the first iteration, ", "it isn't populated here (joined): ", join('-', @date{'YEAR', 'MONTH', 'DAY'}), "\n"; print Dumper(\%date); print "\n\n"; } print "Why????\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex code embedding problem?
by TimToady (Parson) on Mar 13, 2004 at 09:25 UTC | |
|
Re: regex code embedding problem?
by kvale (Monsignor) on Mar 12, 2004 at 23:26 UTC | |
by perlguy (Deacon) on Mar 12, 2004 at 23:45 UTC | |
|
Re: regex code embedding problem?
by TilRMan (Friar) on Mar 13, 2004 at 03:07 UTC | |
|
Re: regex code embedding problem?
by matija (Priest) on Mar 12, 2004 at 23:28 UTC | |
by perlguy (Deacon) on Mar 12, 2004 at 23:40 UTC |