Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Increasing key/values held in a Hash from an Array

by ikegami (Patriarch)
on Mar 17, 2006 at 21:21 UTC ( [id://537584]=note: print w/replies, xml ) Need Help??


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:

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;
or if you don't actually care about the line numbers:
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://537584]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-23 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found