Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Perl - Summarize contents of an array [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
brent_weaver
Guru
Guru


Joined: 01 Jul 2004
Posts: 510
Location: Burlington, VT

PostPosted: Sat Aug 11, 2012 7:23 pm    Post subject: Perl - Summarize contents of an array [SOLVED] Reply with quote

Hey there!

I am looking for help with some logic to create a summary of two strings. The data read in looks like this:

LIVE UNDEFINED
LIVE UNDEFINED
LIVE UNDEFINED
LIVE UNDEFINED
LIVE DATABASE
LIVE SYSTEM
LIVE SYSTEM
TEST DATABASE
TEST DATABASE
TEST DATABASE
TEST DATABASE
TEST DATABASE

Note: The first element above in each line is a database and the second an error message.

What I want to do is have some logic that will summarize this data like:

LIVE has 4 UNDEFINED errors
LIVE has 1 DATABASE error
LIVE has 2 SYSTEM errors
TEST has 5 DATABASE errors

Hopefully this makes sense. Any help is always appreciated, I always learn so much from the G2 community.
_________________
Brent Weaver


Last edited by brent_weaver on Mon Aug 20, 2012 10:54 pm; edited 1 time in total
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Aug 11, 2012 8:27 pm    Post subject: Reply with quote

Something like this, I imagine:
Code:
#/usr/bin/perl

while (<>) {
    chomp;
    @Fields = split;
    $LiveErrors{$Fields[1]}++ if $Fields[0] =~ /^LIVE$/;
    $TestErrors{$Fields[1]}++ if $Fields[0] =~ /^TEST$/;
}

foreach $a (keys %LiveErrors) {
    print "LIVE had $LiveErrors{$a} $a errors.\n";
}

foreach $a (keys %TestErrors) {
    print "TEST had $TestErrors{$a} $a errors.\n";
}
:wink:

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
brent_weaver
Guru
Guru


Joined: 01 Jul 2004
Posts: 510
Location: Burlington, VT

PostPosted: Sat Aug 11, 2012 8:51 pm    Post subject: Reply with quote

John - Thank you VERY much for the information the issue with this code is that there is more than just LIVE and TEST, it is variable.

Let me know your thoughts.
_________________
Brent Weaver
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Aug 11, 2012 8:58 pm    Post subject: Reply with quote

Well, Perl will do hashes of hashes, which would be perfect for that, but I've never done it before. Give me a bit.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Aug 11, 2012 9:10 pm    Post subject: Reply with quote

This is better anyway:
Code:
#!/usr/bin/perl

while (<>) {
    chomp;
    @Fields = split;
    $Errors{$Fields[0]}{$Fields[1]}++;
}

foreach $a (keys %Errors) {
    foreach $b (keys $Errors{$a}) {
        print "$a had $Errors{$a}{$b} $b errors.\n";
    }
}
:D

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.


Last edited by John R. Graham on Mon Aug 20, 2012 4:04 pm; edited 1 time in total
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3922
Location: Hamburg

PostPosted: Sat Aug 11, 2012 9:30 pm    Post subject: Reply with quote

Something like this :
Code:
cat foo | perl -wane 'chomp; $h{$F[0]}->{$F[1]}++; END { foreach $k1 (sort keys %h) { foreach $k2 (sort keys %{$h{$k1}}) { print "$k1 $k2 $h{$k1}->{$k2} \n"; } } }'
LIVE DATABASE 1
LIVE SYSTEM 2
LIVE UNDEFINED 4
TEST DATABASE 5
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Aug 11, 2012 9:37 pm    Post subject: Reply with quote

Useless use of cat. :P

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
Ant P.
Watchman
Watchman


Joined: 18 Apr 2009
Posts: 6920

PostPosted: Sat Aug 11, 2012 11:13 pm    Post subject: Reply with quote

too much reinventing the wheel.
Code:
#!/usr/bin/env perl
use 5.012;
use YAML::XS;

my %list;
while (<>) {
    my ($x, $y) = split;
    $list{$x}{$y}++;
}

say Dump(\%list);
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Aug 11, 2012 11:27 pm    Post subject: Reply with quote

Yes, I'm a relative beginner in Perl. Thanks.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
brent_weaver
Guru
Guru


Joined: 01 Jul 2004
Posts: 510
Location: Burlington, VT

PostPosted: Mon Aug 20, 2012 3:25 pm    Post subject: Reply with quote

Ant P -

Again thank you soo much for this excellent response. This code works perfectly on my mac but when I go to my Oracle Linux server (perl version 5.8.8) it does not like 'use 5.012'. I am not familiar with that module.

Suggestions?

[UPDATE]

I got it - seems that I cannot call say w/o importing 5.012 functionality.

Thanks!
_________________
Brent Weaver


Last edited by brent_weaver on Mon Aug 20, 2012 3:34 pm; edited 1 time in total
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon Aug 20, 2012 3:32 pm    Post subject: Reply with quote

Well then use mine, which is not that version dependent. :wink:

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
brent_weaver
Guru
Guru


Joined: 01 Jul 2004
Posts: 510
Location: Burlington, VT

PostPosted: Mon Aug 20, 2012 10:55 pm    Post subject: Reply with quote

Thank you all for your help. I accomplished exactly what I set out for!
_________________
Brent Weaver
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum