I’m trying to parse out specific values from a text file, and output those to a different file.
I’m using regular expressions stored in a hash (matched up with their descriptive name) to search through a string (scalar), and then storing the discovered values in an array, which is then written out to a file.
I’ve got everything working, except for the searching/extracting part.
(I’ve only just learned Perl in the past couple days, so I wouldn’t be surprised if I was making some really simple mistakes.)
$inputstring = 'Lorem ipsum dolor Date: 20110131 quis semper egestas.';
%myregexhash = ( Date => '/([12][09][0-9][0-9][0-1][0-2][0-9][0-9])/' );
@foundvaluesarray=();
while ( ($thefieldname, $theregex) = each (%myregexhash))
{
if ($inputstring =~ $theregex)
{
push(@foundvaluesarray, "$thefieldname: $&\n");
$inputstring = $';
}
}
print "@foundvaluesarray";
The array fills up with the field names (“Date:”), but not the values I’m looking for (“20110131”).
Any idea what I’m doing wrong?
Make one small change:
Note the use of
qr//, which compiles a regex.You’re new, so I’d recommend a few other changes.
Any non-trivial program should begin with the following front matter:
The
strictpragma has nice benefits such as catching misspelled variable names at compile time and checking your use of references. Thewarningspragma turns on extra warning diagnostics that can alert you to questionable cases in your code.Now must predeclare:
The
= ()is implied in an array or hash declaration, so you don’t see it in idiomatic Perl.You don’t want to use
$&if you can help it because it slows down your entire program.Because you surrounded your pattern with parentheses, the substring that matched is captured in
$1, so grab it from there.Also, the way you chopped off the front of
$inputstringis much more naturally expressed in Perl withs///.Output: