in reply to Re^2: sorting hash by value when value is an array ref
in thread sorting hash by value when value is an array ref

Well, I don't know what all that non-Perl junk is... but I'm inclined to guess that [% FOREACH key IN homeworks %] is iterating over the keys in some other order, and that you're throwing away the result of the sort you so carefully set up. Do you know a way to set the key for iteration based on the contents of @sorted ?

Replies are listed 'Best First'.
Re^4: sorting hash by value when value is an array ref
by beachguy82 (Novice) on Nov 16, 2004 at 03:15 UTC
    I pass the @sorted to the template( it is perl, it's the Template-Toolkit). I pass it like this.
    my $vars = { homework => \%homeworkHash, homeworks => \@sorted, }; my $tt = Template->new({ INCLUDE_PATH => "/inetpub/wwwroot/lib", WRAPPER => 'wrapperInstructor.htm', }); $tt->process('ClassGrades.htm', $vars)||die $tt->error();
    The Template then uses the array named homeworks which is identical to @sorted in the perl script. The [% FOREACH key IN homeworks %] just iterates through the array and prints inserts each element into the hash to get the value.
      If I pass a list of the keys to the template, everything prints out fine. So I'm assuming there must be a problem in my perl script.
        Thank you for you help. As it turned out I had that part of the code in a loop and it was causing the array to be overwritten. Thanks again for your help.
        I'm noticing that template toolkit may do some default alphabetical sorting on hashes. This could explain your problem too.