Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Hash sorting by values

by cajun (Chaplain)
on Aug 08, 2001 at 13:17 UTC ( [id://102992]=perlquestion: print w/replies, xml ) Need Help??

cajun has asked for the wisdom of the Perl Monks concerning the following question:

This code is slightly modified version of miyagawa's code found at CodeRed notifier. Because I wasn't able to understand his regex to get $ip & $datetime, I chose another method to accomplish the task.

What I have thus far, produces output something like this:

2001-08-03 17:57:28 66.120.120.203 2001-08-02 14:15:20 66.121.185.222 2001-08-05 16:51:28 66.24.60.57 2001-08-04 02:35:47 66.30.84.177 2001-08-01 18:52:22 66.74.16.2 2001-08-02 15:38:24 66.92.175.55

What I would like to do is have the output sorted by $datetime (the values of the hash %ip2date, rather than the keys). Thus far, I've not been able to see how to do this.

Here's what I have thus far:

#!/usr/local/bin/perl -w use strict; use Config; use Getopt::Std; use Mail::Sendmail; getopts('f:', \my %opt); my $from = 'me@myaddress.com'; my $to = 'me@myaddress.com'; my %ip2date; # $ip=key $datetime=value while (<>) { next unless m!GET /default\.ida ?[XN]+!; my @datetime=(split)[0,1]; my $datetime=join(' ',@datetime); my $ip=(split)[2]; next if $ip2date{$ip}; $ip2date{$ip} = $datetime; } my $message = join '', map { "$ip2date{$_}\t$_\n" } sort keys %ip2date +; print "$message\n"; sendmail( To => $to, From => $from, Message => $message, Subject => "CodeRed Infection Notification", );

Replies are listed 'Best First'.
Re: Hash sorting by values
by tachyon (Chancellor) on Aug 08, 2001 at 15:02 UTC

    The Perl FAQ can be really useful for answers to questions like this. If you have a look you will see:

    perlfaq4: Data Manipulation

    A quick look at the section on hashes gives you:

    How do I sort a hash (optionally by value instead of key)?

    The FAQs are great. Check 'em out when you have a moment.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Hash sorting by values
by blakem (Monsignor) on Aug 08, 2001 at 13:25 UTC
    In general you can sort a hash by value using a construct such as:
    for my $key (sort {$hash{$a} cmp $hash{$b}} (keys %hash)) { print "Key = $key, Value = $hash{$key}\n"; }
    though I don't see why you want to do it in the code above.

    update ok, I get it now... $message isn't in its final form above... I was wondering why you wanted the body of your email to consist of value sorted elements in the %ip2date hash.

    -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found