in reply to Increasing key/values held in a Hash from an Array
A hash is inappropriate here. Your key is not a key at all. A key is a *unique* way of *identifying* something. A score is neither unique, nor an identity.
my @data = ( [ 12, 'First line of text' ], [ 25, 'Second line of text' ], [ 34, 'Third line of text' ], ); foreach (@scoreWords) { my $re = qr/\b\Q$_\E\b/i; foreach (@data) { $_[0] += $y while $_[1] =~ /$re/g; } }
Update: I either missed the second part of the question, or the OP has been updated. Here's my updated answer:
or if you don't actually care about the line numbers:my @data = ( [ 1, 12, 'First line of text' ], [ 2, 25, 'Second line of text' ], [ 3, 34, 'Third line of text' ], ); foreach (@scoreWords) { my $re = qr/\b\Q$_\E\b/i; foreach (@data) { $_[1] += $y while $_[2] =~ /$re/g; } } print("Line ${$_}[0] has a score of ${$_}[1]\n") foreach sort { $b->[1] <=> $a->[1] } @data;
my @data = ( [ 12, 'First line of text' ], [ 25, 'Second line of text' ], [ 34, 'Third line of text' ], ); foreach (@scoreWords) { my $re = qr/\b\Q$_\E\b/i; foreach (@data) { $_[0] += $y while $_[1] =~ /$re/g; } } print("${$_}[0]: ${$_}[1]\n") foreach sort { $b->[0] <=> $a->[0] } @data;
Update: Changed if // to while //g.
|
|---|