Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

stuck on newb problem

by sweepy838 (Acolyte)
on Apr 28, 2012 at 01:08 UTC ( #967754=perlquestion: print w/ replies, xml ) Need Help??
sweepy838 has asked for the wisdom of the Perl Monks concerning the following question:

i'm still attempting this unscrambler for my challenge, i just need a little help with 1 small thing.. how am i able to check if a string contains the same characters as another string? example:
str1 = "olleh" str2 = "elloh" does str2 contain the same letters as str1 (even if it is in a differe +nt order)

Comment on stuck on newb problem
Download Code
Re: stuck on newb problem
by toolic (Chancellor) on Apr 28, 2012 at 01:25 UTC
    sort the characters of each, then compare with eq:
    use warnings; use strict; my $s = 'hello'; my $x = join '', sort split //, $s; print "$x\n"; __END__ ehllo
      ahh thank you :D such simple solution cant believe i never thought of that :o jeez
Re: stuck on newb problem
by jwkrahn (Monsignor) on Apr 28, 2012 at 02:30 UTC
    my $str1 = "olleh"; my $str2 = "elloh"; if ( $str1 =~ /\A[$str2]+\z/ ) { print "$str1 and $str2 contain the same characters.\n"; }

      for the definition of "same characters" where you mean the same set of unique characters. However the OP probably wouldn't consider:

      my $str1 = "olleh"; my $str2 = "eelooooh";

      to "contain the same characters", but they pass the test.

      True laziness is hard work

      Fast but loose! Try with $str1 = "e" :)

      Edit: keeping the tab open too long obsoletes many a post ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967754]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2015-04-01 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who makes your decisions?







    Results (6 votes), past polls