in reply to string containing characters

TIMTOWTDI.

use strict; use warnings; use Test::More tests => 2; my $goodstr = 'Can you suggest code to determine whether a string cont +ains all of a list of characters in any order?'; my $badstr = 'How to ask better questions using Test::More and sample +data'; my @list = qw/? s a y/; ok haschars ($goodstr, @list), 'Match'; ok !haschars ($badstr, @list), 'No match'; sub haschars { my $str = shift; for my $c (@_) { return 0 if -1 == index $str, $c; } return 1; }

See also How to ask better questions using Test::More and sample data.


🦛

Replies are listed 'Best First'.
Re^2: string containing characters
by ikegami (Patriarch) on Jan 21, 2025 at 22:43 UTC

    Note: Possibly ok, but doesn't handle duplicates in @list.

      Handle how? If you try it with my @list = qw/? ? ? s s s a a a y y y/; instead, it works equally well. Not as efficiently, I grant you but it doesn't croak or give the wrong result.


      🦛

        It gives the wrong result. That list will match your first string even though it doesn't have three ?.

        But again, it's possible that duplicates are disallowed, but the OP did not say they were disallowed.