John W. Krahn wrote:
You may be able to do this by using a tied hash which will actually store the hash's contents in a file.

perldoc DB_File
perldoc AnyDBM_File
perldoc perldbmfilter

Tied hashes look fairly complicated to me, but i'll give them a try ;)

 >
 > #Print hash sorted by Host-IP-Adress
 > foreach $ip ( map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_,
 > (/(\d+)$/)[0] ] } keys %ipload) {

You don't need the list slice because without the /g (global) option the expression can only match once. Your comment says you are sorting by IP address but your code says you are only sorting by the last octet in the address. Did you intend to sort by the complete IP address?


I have to admit that i'm not completly firm with the Schwartzian Transformation, but it does what i want. Because all adresses belong to only one subnet i just need to sort by the last octet and i get:


192.168.0.1
192.168.0.2
...
192.168.0.255

 > ----------------------------------------------------------

This may work as it doesn't slurp the whole file(s) into memory:

use warnings;
use strict;
use Socket;

my %ipload;
{   local @ARGV = @sortlist;

    while ( <> ) {
        next unless / (\d+\.\d+\.\d+\.\d+) \w*\/\w* (\d+) [A-Z]+/
        my $ip = inet_aton( $1 ) or do {
            warn "$1 is an invalid IP address.\n";
            next;
            };
        $ipload{ $1 } += $2
        }
    }

#Print hash sorted by Host-IP-Adress
for ( sort keys %ipload ) {
    my $ip = inet_ntoa( $_ );
    print "$ip = $ipload{$_}\n";
    }

__END__

This gives me "Bad arg length for Socket::inet_ntoa, length is 13, should be 4 at line..."


Thanks

Folker Naumann


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to