in reply to sorting and comparing hashes

This should work for you, if you wanted it sorted by values. If you wanted keys, just change the first for loop to add keys rather than values to the arrays. Enjoy.

my @files; my @plays; for (keys %filelist) { $files[@files] = $filelist{$_}; $plays[@plays] = $playlist{$_}; } my @sorted = (0 .. $#files); @sorted = sort {$files[$a] cmp $files[$b]} @sorted; @files = @files[@sorted]; @plays = @plays[@sorted]; for (my $i=0; $i<@files; $i++) { print "$files[$i] \<\=\> $plays[$i]\n" }
  • Comment on Re: two questions that come together into one grand unified question
  • Download Code

Replies are listed 'Best First'.
Re: Re: two questions that come together into one grand unified question
by Hofmator (Curate) on Oct 15, 2001 at 16:14 UTC
    my @files; my @plays; for (keys %filelist) { $files[@files] = $filelist{$_}; $plays[@plays] = $playlist{$_}; }

    Just a small remark, you don't need a for-loop for that, I'd use values and a hash slice:

    my @files = values %filelist; my @plays = @playlist{ keys %filelist };

    -- Hofmator