Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Print Max Hash

by bartrad (Beadle)
on Jan 30, 2018 at 10:35 UTC ( [id://1208112]=perlquestion: print w/replies, xml ) Need Help??

bartrad has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Print Max Hash
by choroba (Cardinal) on Jan 30, 2018 at 12:23 UTC
    Have you tried checking what $ne really contains? In the sample you posted below, it seems to always be
    $ne = 'ne';

    So, there's only one key, and its value is always the maximum. Can you show what output do you expect?

    Maybe you wanted $fields[15] instead of 17?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Hi, $ne is a multitude of different numbers that I know outputs as expected. Example is 20487273 or 111111. In my sample that I posted I removed the actual values that my input file contained as it's sensitive data and replaced with 'ne'. To be honest I could've replaced it with anything. Field 15 is just a placeholder, 17 definitely prints what I need.

        I'm trying to only print the occurrence once

        Not sure what you are trying to do. If you want to stop multiple records then just use a %seen{key} hash e.g.

        #!perl use strict; my %seen = (); 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]; unless ( $seen{$ne}++ ){ printf "%-15s | %9s | %s \n", $node_ip, $ne, $message; } } } __DATA__ 143599203|No|NACK|ENA||Major|20180129054027||ARM|NSA|PRM|0|DCN|Insuffi +cient SNMP security settings|HN408-CP-1E-I|3176 && 1234|||||||No|| 143599356|No|NACK|ENA|192.168.0.1|Major|20180129054037||CLR|NSA|PRM|0| +DCN|CLEARED: Network element does not respond to SNMP requests|HN4000 +e|3176 && 12345|||||||No|| 143599357|No|NACK|ENA|192.168.0.2|Major|20180129054039||CLR|NSA|PRM|0| +DCN|CLEARED: Insufficient SNMP security settings|HN4000e|3176 && 1234 +56|||||||No|| 143599999|No|NACK|ENA|192.168.0.3|Major|20180129054529||ARM|NSA|PRM|0| +DCN|Network element does not respond to SNMP requests|HN4000e|3176 && + 7890|||||||No|| 143599999|No|NACK|ENA|192.168.0.3|Major|20180129054529||ARM|NSA|PRM|0| +DCN|Network element does not respond to SNMP requests|HN4000e|3176 && + 7890|||||||No|| 143599999|No|NACK|ENA|192.168.0.3|Major|20180129054529||ARM|NSA|PRM|0| +DCN|Network element does not respond to SNMP requests|HN4000e|3176 && + 7890|||||||No||
        poj
Re: Print Max Hash
by Marshall (Canon) on Jan 30, 2018 at 10:40 UTC
    Can you show a few lines of example <INPUT> data?

      Yes of course.

      Few lines from <INPUT>

      143599203|No|NACK|ENA||Major|20180129054027||ARM|NSA|PRM|0|DCN|Insuffi +cient SNMP security settings|HN408-CP-1E-I|3176 && ne|||||||No|| 143599356|No|NACK|ENA|ip|Major|20180129054037||CLR|NSA|PRM|0|DCN|CLEAR +ED: Network element does not respond to SNMP requests|HN4000e|3176 && + ne|||||||No|| 143599357|No|NACK|ENA|ip|Major|20180129054039||CLR|NSA|PRM|0|DCN|CLEAR +ED: Insufficient SNMP security settings|HN4000e|3176 && ne|||||||No|| 143599999|No|NACK|ENA|ip|Major|20180129054529||ARM|NSA|PRM|0|DCN|Netwo +rk element does not respond to SNMP requests|HN4000e|3176 && ne|||||| +|No||

      I've redacted the IP address and replaced with ip and also entered ne in place of a 6-9 digit number as it's sensitive, but hopefully this gives an idea of the format. Here's a sample of the output the script currently produces. 1st and 2nd column are $node_ip and $ne (both redacted) and third column is then $total{$ne}++ that seems to increment just fine and the final column is what I'm trying to call as the max but it increments regardless of what $ne is.

      ip1 5 | Network element does not respond to SNMP requests | + 212 | ip2 213 | Network element does not respond to SNMP requests | + 213 | ip2 214 | Network element does not respond to SNMP requests | + 214 | ip3 4 | Network element does not respond to SNMP requests | + 214 | ip2 215 | Network element does not respond to SNMP requests | + 215 | ip2 216 | Network element does not respond to SNMP requests | + 216 | ip4 1 | Network element does not respond to SNMP requests | + 216 | ip2 | 217 | Network element does not respond to SNMP requests +| 217 | ip2 218 | Network element does not respond to SNMP requests | + 218 | ip2 219 | Network element does not respond to SNMP requests | + 219 |
        What output do you expect?

        The last column doesn't increment "regardless of what $ne is", you can see that 214 is repeated twice, and so is 216. That's because $ne was different for the first of them and the program incremented a different value in the hash, but it wasn't incremented enough to change the max, so the max stayed the same.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        Can you put this data into <code>...</code> blocks?
        "...Few lines from <INPUT>..."

        May i ask where this "few lines" come from? Some Log? External Program? Probably something from the SNMP toolchain? Or some wrapper around one of these? I guess you know what i mean.

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1208112]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-25 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found