View previous topic :: View next topic |
Author |
Message |
brent_weaver Guru
Joined: 01 Jul 2004 Posts: 510 Location: Burlington, VT
|
Posted: Sat Aug 11, 2012 7:23 pm Post subject: Perl - Summarize contents of an array [SOLVED] |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Aug 11, 2012 8:27 pm Post subject: |
|
|
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";
} |
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
brent_weaver Guru
Joined: 01 Jul 2004 Posts: 510 Location: Burlington, VT
|
Posted: Sat Aug 11, 2012 8:51 pm Post subject: |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Aug 11, 2012 8:58 pm Post subject: |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Aug 11, 2012 9:10 pm Post subject: |
|
|
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";
}
} |
- 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 |
|
|
toralf Developer
Joined: 01 Feb 2004 Posts: 3922 Location: Hamburg
|
Posted: Sat Aug 11, 2012 9:30 pm Post subject: |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Aug 11, 2012 9:37 pm Post subject: |
|
|
Useless use of cat.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
Ant P. Watchman
Joined: 18 Apr 2009 Posts: 6920
|
Posted: Sat Aug 11, 2012 11:13 pm Post subject: |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Aug 11, 2012 11:27 pm Post subject: |
|
|
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 |
|
|
brent_weaver Guru
Joined: 01 Jul 2004 Posts: 510 Location: Burlington, VT
|
Posted: Mon Aug 20, 2012 3:25 pm Post subject: |
|
|
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. 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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Aug 20, 2012 3:32 pm Post subject: |
|
|
Well then use mine, which is not that version dependent.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
brent_weaver Guru
Joined: 01 Jul 2004 Posts: 510 Location: Burlington, VT
|
Posted: Mon Aug 20, 2012 10:55 pm Post subject: |
|
|
Thank you all for your help. I accomplished exactly what I set out for! _________________ Brent Weaver |
|
Back to top |
|
|
|