Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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)

Replies are listed 'Best First'.
Re: stuck on newb problem
by toolic (Bishop) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967754]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-04-26 06:38 GMT
Find Nodes?
    Voting Booth?