Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: check if all certain chars are found in a sentence

by varian (Chaplain)
on Aug 27, 2008 at 17:51 UTC ( [id://707231]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://707231]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found