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

 



zacharyjos
New User

Nov 21, 2009, 4:44 PM

Post #1 of 7 (1257 views)
sub hashes and sortedby Can't Post

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)


7stud
Enthusiast

Nov 22, 2009, 4:29 AM

Post #2 of 7 (1236 views)
Re: [zacharyjos] sub hashes and sortedby [In reply to] Can't Post

1) What does "sort by one of hash" mean? Hashes are unordered. You can't sort a hash because it will always remain unordered, so what do you mean?

2) Does 'h1' refer to hash1 or Hash1? Something else?

How about posting,

1) "This is what I have:.....

2) "This is the output I want:....

But remember, you are never going to be able to sort a hash.


Quote
User must choose by whose hashes...


You might want to try something like this:


Code
my $a = { 
car => 'porsche',
watch => 'seiko'
};

my $b = {
job => 'perl',
name => 'zach'
};

my %hash = (
'a' => $a,
'b' => $b
);

my $user_input = 'a';
say $hash{'a'}->{car};



Quote
Hi, my first post.


Not a good start.


(This post was edited by 7stud on Nov 22, 2009, 5:16 AM)


bichonfrise74
Novice

Nov 22, 2009, 5:25 PM

Post #3 of 7 (1196 views)
Re: [zacharyjos] sub hashes and sortedby [In reply to] Can't Post

I'm not sure I understand your question and I suspect that you didn't even write the code that you posted. Anyway, this should help you get started if I understand your question.


Code
#!/usr/bin/perl 

use strict;

my %hash = ( '192.168.0.13' => 555,
'192.168.0.14' => 888,
'192.168.0.15' => 666,
);

print "Enter sort by keys or values? ";
chomp( my $answer = <STDIN> );
print "\n";

print "$_ - $hash{$_}\n"
for ( $answer eq 'keys'
? sort keys %hash
: sort { $hash{$a} <=> $hash{$b} } keys %hash );



--bichonfrise74


zacharyjos
New User

Nov 23, 2009, 3:54 AM

Post #4 of 7 (1188 views)
Re: [bichonfrise74] sub hashes and sortedby [In reply to] Can't Post


Quote
print "Enter sort by keys or values? ";

Thanks, but not keys or values. I must sort by size or time.

Code
print "Enter sort by size or time? ";



7stud
Enthusiast

Nov 23, 2009, 5:16 AM

Post #5 of 7 (1185 views)
Re: [zacharyjos] sub hashes and sortedby [In reply to] Can't Post

Also, next time post in the beginner's forum.


zacharyjos
New User

Nov 23, 2009, 5:32 AM

Post #6 of 7 (1183 views)
Re: [7stud] sub hashes and sortedby [In reply to] Can't Post

Ok sorry, but problem is still not resolve.


zacharyjos
New User

Nov 29, 2009, 1:39 PM

Post #7 of 7 (1155 views)
Re: [bichonfrise74] sub hashes and sortedby [In reply to] Can't Post

Sort by key or value is very easy. I wont only sort by value, but user must chose size or time.

 
 


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

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