in reply to Re: Sorting by the hash value
in thread Sorting by the hash value

"To be civilized is to deny one's nature."
Denying ones nature is the key to being human.

Concerning "Sorting by the hash value":
It is difficult to advise you without knowing exactly what
you are trying to do, but if you are trying to print a
sorted list of values couldn't you just:

@keys = keys{%hash}; foreach $key (@keys) { $value = %hash{$key}; push(@values, $value); } sort(@values); print "@values\n";

Replies are listed 'Best First'.
Re: Re: Re: Sorting by the hash value
by shotgunefx (Parson) on Jul 29, 2002 at 11:09 UTC
    Are you replying to the root node? My kludge is to get around the fact that CGI.pm has the hash ordering (which is no order) when you use the -lables argument. I often need to use labels and tie'ing is easier than sublassing CGI.pm.

    As an aside, your code is overcomplicated.
    print sort values %hash;


    -Lee

    "To be civilized is to deny one's nature."
      Cool. I didn't know that you could do that.

      - cybear

Re: Re: Re: Sorting by the hash value
by Anonymous Monk on Oct 17, 2002 at 22:25 UTC
    Isn't this supposed to be:
    @keys = keys{%hash}; foreach $key (@keys) { $value = $hash{$key}; # notice $ versus % here push(@values, $value); } sort(@values); print "@values\n";

    Otherwise, I think you get an error such as:
    Can't use subscript on private hash at foo.pl line 123, near "$key}" (Did you mean $ or @ instead of %?) BEGIN not safe after errors--compilation aborted at foo.pl line 123.