Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Print Max Hash

by bartrad (Beadle)
on Jan 30, 2018 at 14:35 UTC ( #1208127=note: print w/replies, xml ) Need Help??


in reply to Re^3: Print Max Hash
in thread Print Max Hash

Hi Poj, that worked a treat thank you.

Suppose in my printf I wanted to show the count of how many times said $ne has occurred against the single entry for that $ne, how would I do that? Thanks again!

Replies are listed 'Best First'.
Re^5: Print Max Hash
by poj (Monsignor) on Jan 30, 2018 at 14:55 UTC

    Store the lines in a hash of arrays (HoA) (see perldsc) and print them after all the lines have been scanned/counted

    while ( my $line = <DATA> ) { next if $line =~ /Insufficent|CLEARED/; if ( $line =~ /SNMP/ ) { my @f = split /\|/, $line; my $node_ip = $f[4] || 'N/A'; my $message = $f[13]; my (undef,$ne) = split /[ &]+/,$f[15]; if ( $seen{$ne} ){ ++$seen{$ne}[0]; } else { $seen{$ne} = [ 1, $node_ip, $ne, $message ]; } } } for my $ne ( sort keys %seen ){ printf "%d | %-15s | %9s | %s \n", @{$seen{$ne}}; }
    poj
Re^5: Print Max Hash
by johngg (Canon) on Jan 30, 2018 at 14:46 UTC

    You can't print the total number of occurrences alongside the individual items because you don't know that at the time you print, which is when you see the first of them. However, you could print totals as a summary after you have processed all the records because the %seen hash keeps count for you. Alternatively, defer printing until all records have been read, storing what you want to print as you go in an array (or hash if order is unimportant) that you can tack totals onto at the end of processing.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208127]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-12-16 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (70 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!