Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Need help on hashes.

by moritz (Cardinal)
on Feb 03, 2008 at 20:15 UTC ( #665856=note: print w/replies, xml ) Need Help??

in reply to Need help on hashes.

Although you say that you put the values on a hash, you don't. The code you show us puts them onto an array.
my %countries; while (<DB>){ my @entries = split m/,/, $_; $countries{$entries[1]}++; } for (reverse sort { $countries{$a} <=> $countries{$b} } keys %countrie +s){ print "$_\t\$countries{$_}\n"; }
should do the trick (untested).

Update: no, the \ was not intentional, of course it's print "$_\t$countries{$_}\n";. Thanks cdarke.

Replies are listed 'Best First'.
Re^2: Need help on hashes.
by cdarke (Prior) on Feb 04, 2008 at 09:08 UTC
    Is the \ in front of the $ in the print statement intentional? That gives:
    "US" $countries{"US"} "DE" $countries{"DE"} "RU" $countries{"RU"} "UK" $countries{"UK"} "FR" $countries{"FR"}
    Also it retains the quotes around the country code. I have:
    my %countries; while (<DB>){ my $entry = (split /","/, $_)[1]; $countries{$entry}++; } for (reverse sort { $countries{$a} <=> $countries{$b} } keys %countrie +s){ print "$_\t$countries{$_}\n"; }
      Thank you all for the responses. The example and the explanation are very informative. Hopefully, I'll not experience (i.e. create) any errors when try it out in my script. :-)

      For the record, this is not a homework assignment as apl implies. It's a reasonable thing to suspect but it's incorrect and I don't want the monks to think I'm taking advantage of their help. I'm a network engineer tasked with making sense out of years worth of accumulated syslog data. My code skills (a little C 10 years ago and sporadic uses of perl over the years) are quite limited. The question about whether or not to use a hash was a sincere inquiry as I wasn't sure if there was a more appropriate structure which to employ.

      Thank you all again for your help. It is sincerely appreciated.

      ElMagnifico - Ask for it in your grocer's freezer.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2019-10-19 09:48 GMT
Find Nodes?
    Voting Booth?