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
In reply to Re: Extract a small part of a long sentence using regular expressions
by QM
in thread Extract a small part of a long sentence using regular expressions
by swatzz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |