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

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?
  • Comment on Re^2: how to check which index $x is in an hash

Replies are listed 'Best First'.
Re^3: how to check which index $x is in an hash
by shmem (Chancellor) on May 22, 2007 at 05:16 UTC
    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}