Hi, I'm returning all values from a match for a file and incrementing a hash %total each time the same one occurs. I'm trying to only print the occurrence once by only printing the max of that hash, but it's not working. To be honest, I'm not sure what it's doing with that max value as it all appears to be the same value.

while ( my $line = <INPUT> ) { if ( $line =~ /SNMP/ ) { if ( $line !~ /Insufficient/ && $line !~ /CLEARED/ ) { my @fields = split /[&\|\/]/, $line; $fields[17] =~ s/^\s+//; #REMOVE LEADING SPACES $fields[17] =~ s/\s+$//; #REMOVE TRAILING SPACES #print Dumper \@fields; my ( $node_ip, $ne, $message ) = @fields[ 4, 17, 13 ]; #ASS +IGN FIELDS VARIABLES $total{$ne}++; if ( !$node_ip ) { #FIL +L IN EMPTY SPACES WITH SOMETHING $node_ip = "N/A"; $ne = "N/A"; $total{$ne} = 0; } my $max = max values %total; printf( "%-15s | %9s | %5s | %-55s | %9s | \n", $node_ip, $ne, + $total{$ne}, $message, $max ) # if $total{$ne} == $max; } } } close(INPUT);

Can someone offer any assistance please? I've commented out the if at the end of my printf statement for testing to see exactly what max value is returned and it is the same value regardless of what $ne is.


In reply to Print Max Hash by bartrad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.