in reply to Fast lookup for prefixes in hash

Construct a regex from the hash keys, and do a match with them.
$pat = join '|', sort { length($b) <=> length($a) } keys %prefix; if ($number =~ /^($pat)/) { print "Prefix: $1\n"; }

Replies are listed 'Best First'.
(tye)Re: Fast lookup for prefixes in hash
by tye (Sage) on Oct 31, 2001 at 21:05 UTC

    I think that will probably be a whole lot slower than a (few) hash lookup(s) unless you optimize the regex like is dicussed in this thread: SAS log scanner

            - tye (but my friends call me "Tye")
      I'm well aware of that optimization necessity. I'm putting it in my book somewhere.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        Of course if you ever rewrite the regexp engine, you know what optimization will make the need for manually optimizing that regexp completely not needed...

        (Can I bribe you into trying? :-)