Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Sorting hashes with new value detection

by sulfericacid (Deacon)
on Jan 24, 2006 at 17:57 UTC ( [id://525268]=note: print w/replies, xml ) Need Help??


in reply to Re: Sorting hashes with new value detection
in thread Sorting hashes with new value detection

In short, I am creating a little word game. Each character is worth X points, so I record the word in the hash along with the value of the entire word. That's what the numbers are in the example (not accurate, mind you, it's just basic sample code).

When I print out scores, I'll need to be able to sort by the length of the word and/or sort by the word values. When sorted by value, I need to be able to display all words in specific categories by their value. All 5 pt words are grouped together, as so on.

Thank you.



"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid
  • Comment on Re^2: Sorting hashes with new value detection

Replies are listed 'Best First'.
Re^3: Sorting hashes with new value detection
by Aristotle (Chancellor) on Jan 24, 2006 at 18:15 UTC

    Ah. Do you ever need to look up the value of word based on the word? (Ie. do you need the mapping that your simple hash provides?) If not, I’d simply keep this in the HoA as outlined by Roy Johnson and stick values inside using push @{ $words{ $score } }, $word;

    Otherwise, I’d either keep both structures updated throughout the code (which means putting the push and hash assignment in a sub), or generate one of them out of the other on the fly and demand.

    Makeshifts last the longest.

Re^3: Sorting hashes with new value detection
by thundergnat (Deacon) on Jan 24, 2006 at 20:30 UTC

    Something like this perhaps?

    use warnings; use strict; my %lookup = ( a => 2, b => 4, c => 4, d => 4, e => 1, f => 5, g => 6, h => 3, i => 2, j => 7, k => 7, l => 4, m => 3, n => 3, o => 2, p => 5, q => 11, r => 5, s => 3, t => 3, u => 2, v => 4, w => 7, x => 10, y => 7, z => 9 ); my @scores; while ( my $line = <DATA> ) { chomp $line; my @words = split /\s+/, $line; for my $word (@words) { $word =~ s/[^A-Za-z]//g; next unless length $word; my @letters = split //, $word; my $score; $score += $lookup{ lc $_ } for @letters; push @{ $scores[$score] }, $word; } } for my $score ( 0 .. $#scores ) { if ( defined @{ $scores[$score] } ) { my %words; @words{ @{ $scores[$score] } } = undef; print 'Scored ', $score, ":\n"; print " $_\n" for sort { length $a <=> length $b || $a cmp $b } keys %words; print "\n"; } } __DATA__ In short, I am creating a little word game Each character is worth X points, so I record the word in the hash along with the value of the entire word. That's what the numbers are in the example (not accurate, mind you, it's just basic sample code). When I print out scores, I'll need to be able to sort by the length of the word and/or sort by the word values. When sorted by value, I need to be able to display all words in specific categories by their value. All 5 pt words are grouped together, as so on.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-26 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found