G'day mrras25,
Firstly, there's two issues with how you generate $content.
The individual strings have double-quotes which interpolate and will cause problems with embedded '$' or '@' characters: use single-quotes instead to avoid this.
Any $content .= '...' line where the string starts with a digit will leave the start of that line indistinguishable from the value at the end of the previous line. Consider a situation where $content .= '3PA/Game 16.1 ...' follows $content .= '... Opp 3PM/Game 4.8': $content now contains '... Opp 3PM/Game 4.83PA/Game 16.1 ...'. A way around this is to get rid of every $content .= and just use a single join with a space: my $content = join ' ', '...', '...', '...';.
Back to your question. This code will do what you want with the data you've shown here:
my $re = qr{\s*(.+?)\s+(--|[+-]?\d+[.]\d+)}; my (%stats, @stat_order); while ($content =~ /$re/g) { push @stat_order, $1; $stats{$1} = $2; }
Here's a complete script with all your input showing full output:
Update: I made a small change to the part of the regex matching a potential leading sign: s{[+-]*}{[+-]?}
-- Ken
In reply to Re: Words and Numbers to hash
by kcott
in thread Words and Numbers to hash
by mrras25
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |