in reply to Sorting hashes with new value detection

In the end, I need to be able to sort and rip parts and pieces of this with ease (like a typical hash). Things to keep in mind are; I need to be able to sort by length of characters in the words, and find specific elements which words start with (or end with) specific characters. Ideally, it’d need to be as maintainable as a hash which I’m sure HOAs and AOAs are capable of.

Can you elaborate on your needs? What does the code do?

Makeshifts last the longest.

  • Comment on Re: Sorting hashes with new value detection

Replies are listed 'Best First'.
Re^2: Sorting hashes with new value detection
by sulfericacid (Deacon) on Jan 24, 2006 at 17:57 UTC
    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

      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.

      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.