Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Getting Started / July 2009



Tip: Looking for answers? Try searching our database.

sorting anonymous arrays inside arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
daem0nb0y@yahoo.com - 02 Jul 2009 20:12 GMT
Hi,

If I have a loop that for each run creates

while (<FILE>){
  my $value =~ /^\d/;
  $myhash{$mykey}->{'subkey'} = $value;
}

Normally, if I only want to sort $myhash through it's values, I would do something like:

foreach (sort { $a <=> $b } keys %myhash){

print "$myhash{$_}\n";

}

what if i want to sort the anonymous hash $myhash{$mykey}-> through it's value. In my first loop above, each key of %myhash has a value of an anonymous hash. The key for this anonymous hash is called 'subkey'. the value of course is $value. I want to sort this anonymous hash by $value.

Any idea how to do that?

     

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Jim Gibson - 02 Jul 2009 20:24 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>
> }

That prints the values of %myhash sorted by key. Since the values of %myhash
are references to anonymous hashes, that will not produce anything very
interesting.

> what if i want to sort the anonymous hash $myhash{$mykey}-> through it's
> value. In my first loop above, each key of %myhash has a value of an anonymous
> hash. The key for this anonymous hash is called 'subkey'. the value of course
> is $value. I want to sort this anonymous hash by $value.
>
> Any idea how to do that?

   foreach (
     sort { $myhash{$a}->{subkey} <=> $myhash{$b}->{subkey} }
     keys %myhash
   ) {
       print "$myhash{$_}->{subkey}\n";
   }

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.