in reply to Re^3: Why doesn't exist work with hash slices?
in thread Why doesn't exist work with hash slices?

When all the keys exist, it's a million iterations against one, so it's not surprising. Use something like
my %hash; @hash{ map int rand 1_000_000, 1 .. 1000 } = ();
for a more realistic scenario (still impressive).

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^5: Why doesn't exist work with hash slices?
by LanX (Saint) on Sep 22, 2025 at 13:57 UTC
    Still comparing apple and oranges.

    Handrolling your own my_any or exists_any is not that complicated, only 10-15% slower that List::Util:any .

    And exists_any has a nice syntax too.

    sub my_any (&@) { my $block = shift; $block->() && return !!1 for @_ ; return !!0 } sub exists_any (\%@) { my $h_hash = shift @_; exists $h_hash->{$_} && return 1 for @_; return !!0 } my %hash; my @list = 0 .. 1_000_000; @hash{ map int rand @list, 1 .. 1000 } = (); say my_any { exists $hash{$_} } @list; say exists_any %hash, @list; # note cleaner syntax

    Of course there is no point in reinventing my_any if List::Util is available, but if this semanics is regularly needed a custom function will pay of easily in terms of readability.

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

      The comparison was deliberately apples to oranges because the previous poster suggested using grep.

      List::Util has been in the Perl core since v5.7.3.


      The way forward always starts with a minimal test.