The previous answers seem to address the question asked. The code has many more issues and, as presented, does not run. There are many improvements that could be made, I’m just presenting a minimally cleaned up version of the code, in case that may be of help to the person who asked.

I wonder if this is a practice project from an old Perl course. It’s dated, but was fun to play with.

use strict; use warnings; my @cmdoutput = ( ' Interface IHQ IQD OHQ OQD +RXBS RXPS TXBS TXPS TRTL', '--------------------------------------------------------------------- +--------------------------------------------', '* GigabitEthernet0/0/0 1 0 0 0 11 +5000 111 143000 81 0', '* Gi0/0/0.15 - - - - + - - - - -', '* Gi0/0/0.999 - - - - + - - - - -', '* GigabitEthernet0/0/1 0 0 0 0 9 +1000 32 0 0 0', '* GigabitEthernet0/0/3 0 0 0 0 16 +2000 168 2206000 212 0', '* GigabitEthernet0/1/1 0 0 0 0 485 +0000 1005 9590000 1153 0', '* GigabitEthernet0/1/4 0 0 0 0 210 +5000 200 136000 155 0', '* Te0/3/0 0 0 0 0 1013 +4000 1480 4448000 843 0', ); my %interface_bytes; foreach my $summary (@cmdoutput) { chomp($summary); $summary =~ s/\s+/ /g; my $Star = 0; my $Intf = 0; my $IHQ = 0; my $IQD = 0; my $OHQ = 0; my $OQD = 0; my $RXBS = 0; my $RXPS = 0; my $TXBS = 0; my $TXPS = 0; my $TRTL = 0; my $Track = ""; my $rec = "Sasquatch"; # remove extra spaces my $fields = () = $summary =~ /[\s+,:]/g; # Debug if records are not processing correctly # print "Record contains $fields fields\n"; if ( $fields == 0 ) { print "No fields\n"; next; } elsif ( $fields == 10 ) { ( $Star, $Intf, $IHQ, $IQD, $OHQ, $OQD, $RXBS, $RXPS, $TXBS, $TXPS +, $TRTL ) = split( ' ', $summary ); if ( $Star ne "*" ) { next; } elsif ( $RXBS =~ /\D/ ) { next; } elsif ( $TXBS =~ /\D/ ) { next; } else { $Track = join "<<-", $rec, $Intf; $interface_bytes{$Track} += $RXBS; $Track = join "->>", $rec, $Intf; $interface_bytes{$Track} += $TXBS; } } else { print STDERR "Danger Will Robinson - my sensors detect an invisible hole that may c +onsume you\n"; } } my $key = ""; my @keys = (); my $lastid = 4; # sort by value and put the keys in an array (Ascending sort) @keys = sort { $interface_bytes{$a} <=> $interface_bytes{$b} } keys %interfa +ce_bytes; # foreach $key ( @keys[ 0 .. $lastid ] ) { foreach $key (@keys) { if ( not defined $key ) { next; } # printf("%-45s %-6d\n","\t".$key,$interface_bytes{$key}); # my $number = $interface_bytes{$key}; # $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g; # printf("%-20s %-8s\n",$key,$number); printf( "%-10s %-6s\n", "\t" . $key, $interface_bytes{$key} ); }

In reply to Re: Sort never returns data in right order by stall
in thread Sort never returns data in right order by jsmith118

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.