in reply to how to check which index $x is in an hash

my @sorted_indices = sort {$a <=> $b} keys %upload;

Then iterate over @sorted_indices.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: how to check which index $x is in an hash
by Anonymous Monk on May 21, 2007 at 21:50 UTC
    Right, that's my question though. I have an array of hash keys. If I have an array with 1,2,5,7,10, how do I determine what index 5 is? I know it's 2, but how can the script know?
      how can the script know?

      The script doesn't know anything, ever. It's you who knows, or doesn't, and the script reflects that knowledge.

      If I have an array with 1,2,5,7,10, how do I determine what index 5 is? I know it's 2

      You make another hash, keyed with the contents of your array slots, whose values are their indices:

      # (merging previous post) my @sorted_indices = sort {$a <=> $b} keys %upload; my %lookup_hash; @lookup_hash{@sorted_indices} = 0 .. $#sorted_indices;

      Now you lookup e.g. 5 in %lookup_hash:

      my $i = $lookup_hash{5}; print $x,"\n"; # prints 2 # get the next element from the hash my $next = $sorted_indices[$x + 1]; # $next holds 7 # get your image my $image = $uploads{$next};

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}