in reply to Re^4: Why doesn't exist work with hash slices?
in thread Why doesn't exist work with hash slices?
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Why doesn't exist work with hash slices?
by 1nickt (Canon) on Sep 22, 2025 at 14:25 UTC |