in reply to Words and Numbers to hash

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:

Script:

#!/usr/bin/env perl -l use strict; use warnings; my $content = "Points/Game 80.5 Opp Points/Game 69.7 Avg Score Margi +n +10.8 Opp Avg Score Margin -10.8 Assists/Game 16.5 Opp Assists/G +ame 12.2 Total Rebounds/Gm 39.3 Opp Total Rebounds/Gm 36.7 Assists +/FGM 0.557 Opp Assists/FGM 0.472 Assists/Turnover 1.259 Opp Assist +s/Turnover 0.712"; $content .= "Effective FG 51.0 Opp Effective FG 47.5 FTA/FGA 0.409 + Opp FTA/FGA 0.348 Free Throw 65.4 Opp Free Throw 64.6 Three Po +int 29.2 Opp Three Point 30.4 Two Point 53.6 Opp Two Point 48. +2 Shooting 47.3 Opp Shooting 43.5 Shooting Efficiency 1.080 Opp + Shooting Efficiency 1.007 FGM/Game 29.5 Opp FGM/Game 25.8 FGA/Gam +e 62.5 Opp FGA/Game 59.3 3PM/Game 4.7 Opp 3PM/Game 4.8 3PA/Game 1 +6.1 Opp 3PA/Game 15.9 FTM/Game 16.7 Opp FTM/Game 13.3 FTA/Game 25 +.5 Opp FTA/Game 20.6 1st Half Pts/Gm 39.8 Opp 1st Half Pts/Gm 33.6 2nd Half Pts/Gm 40.6 +Opp 2nd Half Pts/Gm 36.1 OT Pts/Gm -- Opp OT Pts/Gm -- "; $content .= "Off Rebounds/Gm 12.1 Opp Off Rebounds/Gm 10.8 Def Rebou +nds/Gm 23.2 Opp Def Rebounds/Gm 21.2 Off Rebound 36.3 Opp Off Reb +ound 31.7 Def Rebound 68.3 Opp Def Rebound 63.7 Blocks/Game 5.4 Opp Blocks/Game 4.7 Block 9.1 Opp Block 7.5 Steals/Game 10.1 Opp Steals/Game 7.7 Steals/Play 11.7 Opp Steals/Pl +ay 8.8 Turnovers/Game 13.1 Opp Turnovers/Game 17.1 Turnovers/Play 14.9 Opp + Turnovers/Play 19.8"; $content .= "Personal Fouls/Gm 18.5 Opp Personal Fouls/Gm 20.8 Perso +nal Fouls/Play 21.4 Opp Personal Fouls/Play 23.7"; my $re = qr{\s*(.+?)\s+(--|[+-]?\d+[.]\d+)}; my (%stats, @stat_order); while ($content =~ /$re/g) { push @stat_order, $1; $stats{$1} = $2; } print "$_: $stats{$_}" for @stat_order;

Output:

Points/Game: 80.5 Opp Points/Game: 69.7 Avg Score Margin: +10.8 Opp Avg Score Margin: -10.8 Assists/Game: 16.5 Opp Assists/Game: 12.2 Total Rebounds/Gm: 39.3 Opp Total Rebounds/Gm: 36.7 Assists/FGM: 0.557 Opp Assists/FGM: 0.472 Assists/Turnover: 1.259 Opp Assists/Turnover: 0.712 Effective FG: 51.0 Opp Effective FG: 47.5 FTA/FGA: 0.409 Opp FTA/FGA: 0.348 Free Throw: 65.4 Opp Free Throw: 64.6 Three Point: 29.2 Opp Three Point: 30.4 Two Point: 53.6 Opp Two Point: 48.2 Shooting: 47.3 Opp Shooting: 43.5 Shooting Efficiency: 1.080 Opp Shooting Efficiency: 1.007 FGM/Game: 29.5 Opp FGM/Game: 25.8 FGA/Game: 62.5 Opp FGA/Game: 59.3 3PM/Game: 4.7 Opp 3PM/Game: 4.8 3PA/Game: 16.1 Opp 3PA/Game: 15.9 FTM/Game: 16.7 Opp FTM/Game: 13.3 FTA/Game: 25.5 Opp FTA/Game: 20.6 1st Half Pts/Gm: 39.8 Opp 1st Half Pts/Gm: 33.6 2nd Half Pts/Gm: 40.6 Opp 2nd Half Pts/Gm: 36.1 OT Pts/Gm: -- Opp OT Pts/Gm: -- Off Rebounds/Gm: 12.1 Opp Off Rebounds/Gm: 10.8 Def Rebounds/Gm: 23.2 Opp Def Rebounds/Gm: 21.2 Off Rebound: 36.3 Opp Off Rebound: 31.7 Def Rebound: 68.3 Opp Def Rebound: 63.7 Blocks/Game: 5.4 Opp Blocks/Game: 4.7 Block: 9.1 Opp Block: 7.5 Steals/Game: 10.1 Opp Steals/Game: 7.7 Steals/Play: 11.7 Opp Steals/Play: 8.8 Turnovers/Game: 13.1 Opp Turnovers/Game: 17.1 Turnovers/Play: 14.9 Opp Turnovers/Play: 19.8 Personal Fouls/Gm: 18.5 Opp Personal Fouls/Gm: 20.8 Personal Fouls/Play: 21.4 Opp Personal Fouls/Play: 23.7

Update: I made a small change to the part of the regex matching a potential leading sign: s{[+-]*}{[+-]?}

-- Ken