Bizarre Question

corliss at alaskapm.org corliss at alaskapm.org
Tue Apr 17 16:24:26 CDT 2001


On Tue, 17 Apr 2001, Leif Sawyer wrote:

> What I'm trying to do, is print out a list of ports, by protocol,
> for each ACL.  And i'd like the ports sorted by the number of packets,
> descending.

One question:  do you want each subset to be sorted by packet count, or the
entire list sorted by packet count?  Obviously, that makes a huge difference.

To just sort it by acl, protocol, and packet count, in that order, something
like this would do:

foreach $acl (keys %data) {
	foreach $proto (keys %{$data{$acl}}) {
		foreach $port (sort { $data{$acl}{$proto}{$b} <=> 
			$data{$acl}{$proto}{$a} } keys %{$data{$acl}{$proto}}) {
			
			# Do what you need
			
		}
	}
}

Otherwise, I'd just collapse the entire hash and sort:

foreach $acl (keys %data) {
	foreach $proto (keys %{$data{$acl}}) {
		foreach $port (sort { $data{$acl}{$proto}{$b} <=> 
			$data{$acl}{$proto}{$a} } keys %{$data{$acl}{$proto}}) {
			
			$new{"$acl:$proto:$port"} = $data{$acl}{$proto}{$port};
			
		}
	}
}

foreach (sort { $new{$b} <=> $new{$a} } keys %new) {
	print "@{[ split(/:/, $_) ]} $new{$_}\n";
}

I'm sure there's probably a better way, but brute-forcing it works.  ;-)

	--Arthur Corliss
	  Perl Monger/Alaska Perl Mongers
	  http://www.alaskapm.org/

=================================================
Mailing list info:  If at any time you wish to (un|re)subscribe to
the list send the request to majordomo at hfb.pm.org.  All requests
should be in the body, and look like such
                  subscribe anchorage-pm-list
                  unsubscribe anchorage-pm-list



More information about the Anchorage-pm mailing list