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:
sorting on hashed records

 



long_beach_john
Deleted

Dec 7, 2000, 11:31 PM

Post #1 of 6 (584 views)
sorting on hashed records Can't Post

any help much appreciated:
$hash->{1}={
name=>'john',
children=>7
};
$hash->{7}={
name=>'billy',
children=>2
};
$hash->{4}={
name=>'newman',
children=>0
};
how do i list name by number of children?
even better would be get an array of keys (%$hash) that i could foreach loop through

thanks!!!!!


sleuth
Enthusiast / Moderator

Dec 8, 2000, 12:18 AM

Post #2 of 6 (584 views)
Re: sorting on hashed records [In reply to] Can't Post

 I found your question to be interesting so I really wanted to find a solution, by looking at your data, I suppose that all the numbers are unique, if that's the case then I got this to work, it's a REALY round a bout way and I hope someone else can come up with something better, but I just had to try, heh.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%hash = (
newman => '8',
laura => '2',
jeff => '6',
paul => '8'
);
foreach $t (keys %hash){
$names{$hash{$t}} = "$t";
}
foreach $i (sort{$a <=> $b} values %hash){
print "$names{$i} = $i\n";
}
</pre><HR></BLOCKQUOTE>

I'm going to try a few other things also and See if I can come up with anything, I always seem to take the wierdest route first...

Sleuth

[This message has been edited by sleuth (edited 12-08-2000).]


sleuth
Enthusiast / Moderator

Dec 8, 2000, 12:41 AM

Post #3 of 6 (584 views)
Re: sorting on hashed records [In reply to] Can't Post

 
I made a little progress after reading more hash sort docs,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%hash = (
newman => '8',
laura => '2',
jeff => '6',
phil => '1'
);
%hash = reverse %hash;
foreach $i (sort{$a <=> $b} keys %hash){
print "$hash{$i} = $i\n";
}
</pre><HR></BLOCKQUOTE>

Sleuth


sleuth
Enthusiast / Moderator

Dec 8, 2000, 1:13 AM

Post #4 of 6 (584 views)
Re: sorting on hashed records [In reply to] Can't Post

 
All Right!!, Just shows ya that persistance pays off.

I got it, regardless of duplicated numbers.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

%hash = (
newman => '8',
laura => '2',
jeff => '6',
phil => '8'
);
@keys = sort {
$hash{$a} cmp $hash{$b}
}
keys %hash; # and by value
$c=@keys;
foreach $number (0..$c){
print "$keys[$number] = $hash{$keys[$number]}\n";
}
</pre><HR></BLOCKQUOTE>

Man, that's cool,

Sleuth


rGeoffrey
User / Moderator

Dec 8, 2000, 11:33 AM

Post #5 of 6 (584 views)
Re: sorting on hashed records [In reply to] Can't Post

This might be useful...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my %hash = ( 1 => {name=>'john', children=>7},
7 => {name=>'billy', children=>2},
8 => {name=>'fred', children=>2},
4 => {name=>'newman',children=>0},
);

my @ordered_keys = sort {$hash{$a}{'children'} <=> $hash{$b}{'children'}} keys (%hash);

print (join ("\n", map { $_ . '=' . $hash{$_}{'name'} } @ordered_keys), "\n");
</pre><HR></BLOCKQUOTE>

The array @ordered_keys is the keys to %hash in order of the number of children. My print statement prints the key and then the name, but that can be changed as needed.


long_beach_john
Deleted

Dec 8, 2000, 11:39 AM

Post #6 of 6 (584 views)
Re: sorting on hashed records [In reply to] Can't Post

you all rock!
thanks

 
 


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

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