jsmith118 has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, Can anyone tell me how to fix this code? When I run this against the same testdata from a file, it always returns the sort in different orders, never in ascending or descending numeric.
foreach my $summary (@cmdoutput) { chomp($summary); $summary =~ s/\s+/ /g; my $Intf = $IHQ = $IQD = $OHQ = $OQD = $RXBS = $RXPS = $TXBS = $TX +PS = $TRTL = $Track = 0; select STDOUT; # 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 ($IHQ eq "-") { next;} elsif ($Intf eq "Interface") {next;} else { $Track = join "<<-", $rec,$Intf; $interface_bytes{$Track} += $RXBS; $Track = join "->>", $rec,$Intf; $interface_bytes{$Track} += $TXBS; } } } # 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 %inte +rface_bytes; foreach $key (@keys[0..$lastid]) { 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}); }
Testfile: intf-output.txt Interface IHQ IQD OHQ OQD R +XBS RXPS TXBS TXPS TRTL ---------------------------------------------------------------------- +------------------------------------------- * GigabitEthernet0/0/0 1 0 0 0 115 +000 111 143000 81 0 * Gi0/0/0.15 - - - - + - - - - - * Gi0/0/0.999 - - - - + - - - - - * GigabitEthernet0/0/1 0 0 0 0 91 +000 32 0 0 0 * GigabitEthernet0/0/3 0 0 0 0 162 +000 168 2206000 212 0 * GigabitEthernet0/1/1 0 0 0 0 4850 +000 1005 9590000 1153 0 * GigabitEthernet0/1/4 0 0 0 0 2105 +000 200 136000 155 0 * Te0/3/0 0 0 0 0 10134 +000 1480 4448000 843 0 Results: PS C:\perf> perl intf-summ.pl intf-output.txt Output from new-telnet.pl --- Input file: intf-output.txt End of Input file listing 10 lines RSA_4500<<-IHQ 0 RSA_4500<<-GigabitEthernet0/1/4 2105000 RSA_4500<<-GigabitEthernet0/1/1 4850000 RSA_4500<<-GigabitEthernet0/0/1 91000 RSA_4500->>GigabitEthernet0/0/0 143000 PS C:\perf> perl intf-summ.pl intf-output.txt Output from new-telnet.pl --- Input file: intf-output.txt End of Input file listing 10 lines RSA_4500->>IHQ 0 RSA_4500->>Te0/3/0 4448000 RSA_4500<<-GigabitEthernet0/1/1 4850000 RSA_4500<<-GigabitEthernet0/0/3 162000 RSA_4500->>GigabitEthernet0/1/4 136000 PS C:\perf> perl intf-summ.pl intf-output.txt Output from new-telnet.pl --- Input file: intf-output.txt End of Input file listing 10 lines RSA_4500->>IHQ 0 RSA_4500<<-GigabitEthernet0/1/4 2105000 RSA_4500<<-Te0/3/0 10134000 RSA_4500->>Te0/3/0 4448000 RSA_4500<<-IHQ 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort never returns data in right order
by hippo (Archbishop) on Sep 05, 2017 at 14:39 UTC | |
by jsmith118 (Novice) on Sep 05, 2017 at 14:53 UTC | |
by ikegami (Patriarch) on Sep 05, 2017 at 19:20 UTC | |
|
Re: Sort never returns data in right order
by Eily (Monsignor) on Sep 05, 2017 at 14:57 UTC | |
|
Re: Sort never returns data in right order
by dave_the_m (Monsignor) on Sep 05, 2017 at 14:39 UTC | |
|
Re: Sort never returns data in right order
by talexb (Chancellor) on Sep 05, 2017 at 14:57 UTC | |
|
Re: Sort never returns data in right order
by stall (Novice) on Sep 06, 2017 at 07:08 UTC | |
by afoken (Chancellor) on Sep 06, 2017 at 08:16 UTC | |
by stall (Novice) on Sep 06, 2017 at 13:17 UTC |