Vincent:

On Fri, Jul 17, 2015 at 9:11 AM, Vincent Lequertier <s...@riseup.net> wrote:
> Hi,

Hello,

> I have the following structure :
>
> $hash{$date} = {
>                     'ip'     => $ip,
>                     'action'   => $action,
>                 };
>
> witch produce data like :
>
> $VAR1 = '[15/Jul/2015:10:30:03 +0200]';
> $VAR2 = {
>           'ip'     => 'xxx.xxx.xxx.xxx',
>           'action' => 'GET xxx'
>         };
>
> and an array of ip addresses, say @ip
>
> My question is how can I display the content of %hash in the
> order of @ip, assuming %hash has the same length as @ip ?

Unfortunately this data structure isn't ideal for doing this.
You'll have to search the hash structure for the matching IP
addresses.

You may wish to transform the data structure first (or simply
store it differently in the first place if this is all you need
it for).

If the hash keys were IP addresses then you could simply loop
over the elements of @ip (some would argue arrays and hashes
should have plural names) and use the current IP address to
address the current hash element from '%hash' (note: "hash" is
usually a poor name for a variable).

use strict;
use warnings;

use Data::Dumper;

sub cmp_ip_addresses {
    my @parts = map { split /\./, $_ } $a, $b;

    for my $i (0..4) {
        my $result = $parts[$i] <=> $parts[$i + 4];

        if ($result) {
            return $result;
        }
    }

    return 0;
}

# The resulting structure will be inverted so the keys are IPs
# and the values are references to hashes containing a date and
# action (you could go further and store actions by date, or
# dates by action; depends how you want the data shaped).

sub transform_date_logs_to_ip_logs {
    my ($logs) = @_;
    my %logs;

    while(my ($date, $entry) = each(%{$logs})) {
        my ($ip, $action) = @{$entry}{qw/ip action/};

        push @{$logs{$ip}}, { date => $date, action => $action};
    }

    return \%logs;
}

my @ip_addresses = qw(
    192.168.200.220
    1.0.0.127
    192.168.90.221
);

my %date_logs = (
    '15/Jul/2015:10:30:01 +0200' => {
        ip => '1.0.0.127',
        action => 'GET http://1.0.0.127/foo',
    },
    '15/Jul/2015:10:30:04 +0200' => {
        ip => '192.168.90.221',
        action =>  'GET http://192.168.90.221/bar',
    },
    '15/Jul/2015:10:30:05 +0200' => {
        ip => '192.168.200.220',
        action => 'GET http://192.168.200.220/baz',
    },
    '15/Jul/2015:10:30:03 +0200' => {
        ip => '1.0.0.127',
        action => 'GET http://1.0.0.127/foo/bar',
    },
);

my $ip_logs = transform_date_logs_to_ip_logs(\%date_logs);

for my $ip (sort { cmp_ip_addresses() } @ip_addresses) {
    my $entries = $ip_logs->{$ip};

    for my $entry (@{$entries}) {
        my ($date, $action) = @{$entry}{qw/date action/};

        print "Date: $date, IP Address: $ip, Action: $action\n";
    }
}

__END__


Regards,


-- 
Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bambams.ca/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to