in reply to A Simple hash question
Now, if you had the original list assigned into an array, that would be a different story:%pets = ( 'pony' => 2, 'cat' => 2, 'dog' => 2 );
Hope that helps!#!/usr/bin/perl -w use strict; my @pets = ("dog", 1, "cat", 2, "pony", 2, "dog", 2); my %pets; while (@pets) { my $key = shift @pets; my $val = shift @pets; $pets{$key} += $val; } foreach (sort keys %pets) { print "$_: $pets{$_}\n"; }
-- zigdon
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: A Simple hash question
by chinamox (Scribe) on Oct 01, 2006 at 15:45 UTC |