CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: sub hashes and sortedby: Edit Log



zacharyjos
New User

Nov 21, 2009, 4:44 PM


Views: 2447
sub hashes and sortedby

Hi, my first post. (sorry for my previous)
I want print two raports ALLOW and DENIED. Both sorted by size or time. User must choose size or sum time. I don't known how use $sortBy.


Code
# size and sum access time for ALLOWED sites 
%hash1 = ( '192.168.0.13' => 555, '192.168.0.14' => 666, '192.168.0.15'=> 888);
%hash2 = ( '192.168.0.13' => 999, '192.168.0.14' => 444, '192.168.0.15'=> 777);

# size and sum of access time for DENIED sites;
%Hash1 = ( '192.168.0.13' => 22, '192.168.0.14' => 33, '192.168.0.15'=> 11);
%Hash2 = ( '192.168.0.13' => 99, '192.168.0.14' =>88, '192.168.0.15'=> 77);

# sort output by size (hash1, Hash1) or sort by sum time acces (hash2, Hash2)
$sortBy = "hash1";

print "raport for ALLOWED sites\n";
func($sortBy, \%hash1, \%hash2);
print "raport for DENIED sites\n";
func($sortBy, \%Hash1, \%Hash2);


sub func{
my ($z, $h1, $h2) = @_;
#print Dumper $h1;
foreach $key (sort {$$h1{$b} <=> $$h1{$a}} keys %$h1) {
print $key.": ".$$h1{$key};
print " MB, ".$$h2{$key}." min\n";
}
}

output sorted by size ... foreach $key (sort {$$h2{$b} <=> $$h2{$a}} keys %$h2)...

Code
raport for ALLOWED sites 
192.168.0.15: 888 MB, 777 min
192.168.0.14: 666 MB, 444 min
192.168.0.13: 555 MB, 999 min
raport for DENIED sites
192.168.0.14: 33 MB, 88 min
192.168.0.13: 22 MB, 99 min
192.168.0.15: 11 MB, 77 min

output sorted by summary time

Code
raport for ALLOWED sites 
192.168.0.13: 555 MB, 999 min
192.168.0.15: 888 MB, 777 min
192.168.0.14: 666 MB, 444 min
raport for DENIED sites
192.168.0.13: 22 MB, 99 min
192.168.0.14: 33 MB, 88 min
192.168.0.15: 11 MB, 77 min


but how user may choose sort by?


(This post was edited by zacharyjos on Nov 23, 2009, 4:01 AM)


Edit Log:
Post edited by zacharyjos (New User) on Nov 22, 2009, 4:04 PM
Post edited by zacharyjos (New User) on Nov 22, 2009, 4:05 PM
Post edited by zacharyjos (New User) on Nov 23, 2009, 4:01 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives