in reply to check if all certain chars are found in a sentence

In the spirit of TMTOWTDI using a hash provides for an alternative quick approach and e.g. could tell you required items that are missing:
#!/usr/bin/perl use strict; use warnings; my $sentence = "abxcd zwe rrv"; my $wantedLetters = "tzxv"; my %required = map {$_ => 1} split //,$wantedLetters; map delete $required{$_}, split //, $sentence; if (keys %required) { print "required yet missing: ",keys %required,"\n"; } else { print "all requirements were met!\n"; }

Replies are listed 'Best First'.
Re^2: check if all certain chars are found in a sentence
by RMGir (Prior) on Aug 27, 2008 at 21:11 UTC
    I wonder if that could be re-written w/ hash slices?
    #!/usr/bin/perl use strict; use warnings; my $sentence = "abxcd zwe rrv"; my $wantedLetters = "tzxv"; my %required; @required{split //,$wantedLetters}=(); delete @required{split //, $sentence}; if (keys %required) { print "required yet missing: ",keys %required,"\n"; } else { print "all requirements were met!\n"; }
    Quick test... Yup, that works! Fun...

    Mike