in reply to Extract a small part of a long sentence using regular expressions
Update: Fixed the regex to capture correctly by putting parens around the list inside the literal parens, and ignoring captures on the internal group.
# First, just grab the list if (my ($list) = $line =~ /\((\d+(?:,\d+)*)\)/) { # split the list by commas, assuming no whitespace my @list = split ',', $list; # initialise the magic alpha incrementer key my $key = 'a'; my %hash; for my $value (@list) { next unless $value; $hash{$key} = $value; # increment magically ++$key; } do_something_with(%hash); }
Then the question is whether you need to do something with %hash for each line, or accumulate these across the whole file. If it's file level, move the my %hash; to before the if, and the do_something_with(%hash) after the if block.
Also, do_something_with(%hash) might be better as a hash reference:
do_something_with(\%hash);
-QM
--
Quantum Mechanics: The dreams stuff is made of
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extract a small part of a long sentence using regular expressions
by AnomalousMonk (Archbishop) on Dec 02, 2014 at 17:40 UTC | |
by swatzz (Novice) on Dec 03, 2014 at 07:45 UTC | |
|
Re^2: Extract a small part of a long sentence using regular expressions
by Anonymous Monk on Dec 02, 2014 at 16:00 UTC | |
by Myrddin Wyllt (Hermit) on Dec 02, 2014 at 23:22 UTC |