in reply to Hash versus chain of elsifs

The only thing in Perl which might be able to rival a hash look-up is a or-regex using the Tree° Trie optimization, especially if you have repeated patterns.

$in =~ m/^($STR1|$STR2|$STR3|...)$/

You should provide more details, for a definitive answer.

But a trie will always beat if-else with eq comparisons.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

updates

°) that's not the name... how was it called again... ah ... s/Tree/Trie/

> and does the "best" approach vary by scale?

yes. see ${^RE_TRIE_MAXBUF}

Replies are listed 'Best First'.
Re^2: Hash versus chain of elsifs
by dsheroh (Monsignor) on Nov 23, 2021 at 08:32 UTC
    On the regex suggestion, once upon a time, I did a project which used Regexp::Assemble to take a long list of rexeges and combine them all into a single, monstrously unreadable, regex that would tell me in one shot whether any matched. I seem to recall hearing at some point that similar functionality had been added to the Perl core, but don't recall details.

    In any case, if you're looking for fixed strings rather than regex patterns, substring matches, etc., I would still expect hashes to be faster, but something based on Regexp::Assemble or a similar technique could be close enough to warrant a benchmark. And you might still want to go that route even if it's a little slower in order to gain the extra flexibility in how you match.