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

Hi, if I understood correctly your problem you could just create a hash whose keys are made of the concatenated start and end, with a appropriate separator, e.g. "7-12". The values can be anything, it could be 1. Then you just check for the existence of the hash for the concatenated pair of numbers.
if (exists $hash{"start-$end"} ) { # do something }

Replies are listed 'Best First'.
Re^2: Request help on optimizing a loop for looking up numbers
by VincentK (Beadle) on Dec 11, 2013 at 16:16 UTC

    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.
      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>

      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.)