in reply to Re: Request help on optimizing a loop for looking up numbers
in thread Request help on optimizing a loop for looking up numbers

Thank you for your reply Laurent_R.

That is close to what I want. Again, I probably did not do a good enough job explaining what I want. Sorry. The look up you have is close, but I want a check similar to yours that will check for the numbers between the range. Given '7-12', the code will check for 7,8,9,10,11, and 12.

Is there something I can use that is similar to your check, but will essentially look for this ?

 if ( exists $hash{7} || exists $hash{8}... exists $hash{12} )

Ideally

 if ( exists $hash{7..12} )

Thanks.

Replies are listed 'Best First'.
Re^3: Request help on optimizing a loop for looking up numbers
by Laurent_R (Canon) on Dec 11, 2013 at 16:49 UTC
    Hash slices might be what you need. This is an example of how they work demonstrated under the Perl debugger:
    DB<1> @hash{qw/one two three/} = qw /1 2 3/; DB<2> x \%hash 0 HASH(0x302ee308) 'one' => 1 'three' => 3 'two' => 2 DB<3>
Re^3: Request help on optimizing a loop for looking up numbers
by Anonymous Monk on Dec 11, 2013 at 16:51 UTC

    You can do something like

    if (grep exists $hash{$_}, 7..12) {

    but it's not necessarily any faster.

    A hash is an unordered data type, so there's no easy way to search for a range. (There are some "ordered hash" packages on cpan, but that's really a misleading name.)