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

Re: Anagrams & Letter Banks

by Discipulus (Canon)
on Oct 27, 2017 at 19:51 UTC ( [id://1202190]=note: print w/replies, xml ) Need Help??


in reply to Anagrams & Letter Banks

Hi dominik_t

> Given a list whose elements are strings (containing only letters, as they are actually English-language words), how would one print that list only on the condition that at least one of the strings in the list contains no repeated letters?

Counting repetitions or not repetitions is a call for a hash. The following iterates over lists and, using the $has_unique switch, search if one word of the list is built with just different characters.

This is done with the %chars hash: $chars{$_}++ for ($word =~ /./g) where the regex in list context returns all chars and the respective value in the hash is augmented by one.

Then if the length of the $word is equal to the number of keys of the hash the word must be built with different characters, so the switch $has_unique is turned on and after all word in the list were processed the list is printed only if the switch is on.

use strict; use warnings; my $good = [qw( allo mallo malo)]; my $bad = [qw( tillo sillo sallo)]; foreach my $list ($good,$bad){ my $has_unique; foreach my $word(@$list){ my %chars; $chars{$_}++ for ($word =~ /./g); if (scalar keys %chars == length $word){ # print "$word has no repeated letters"; $has_unique++; } } print +(join ' ', @$list),"\n" if $has_unique; } # output allo mallo malo

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Anagrams & Letter Banks
by dominick_t (Acolyte) on Oct 27, 2017 at 20:27 UTC
    Thank you, Discipulus. I need clarification on at least a couple of things. If (allo mallo malo) is a list, why does it appear to be a scalar with my $good?

    Also, with regard to the %chars hash, are you saying that a new hash is made for each word in the list, and that the keys of that hash are the individual characters in the word? And after it's parsed a whole word, the value corresponding to a key is the number of times it occurs? (Does the ++ make that happen? I guess I'm not seeing how the values are added to the hash.) So then this works by looking at the number of keys, which would match the length of the word if and only if the word had no repeated letters.
      hello again dominick_t

      An explicit array is @arr = qw(a b x) and an anonymous one is $array_ref = [qw(c d e)]

      Use print and/or Data::Dumper or better Data::Dump to se in action:

       my $array_ref = [qw(c d e)]; print Dumper $array_ref;

      The doc covering this is perlref

      > %chars hash, are you saying that a new hash is made for each word in the list, and that the keys of that hash are the individual characters in the word?

      Yes! try it to see; print is your first debugging tool:

      use strict; use warnings; my $good = [qw( allo mallo malo)]; my $bad = [qw( tillo sillo sallo)]; foreach my $list ($good, $bad){ print "got list [@{$list}]\n"; my $has_unique; foreach my $word(@$list){ print "analizing word [$word]\n"; my %chars; foreach my $char ($word =~ /./g){ print "\tGot char [$char]\n"; $chars{$char}+= 1; } print "\tchars count is:\n"; print map{"\t$_ = $chars{$_} "}keys %chars; print "\n"; if (scalar keys %chars == length $word){ print "\t$word has no repeated letters (keys of \%chars + are equal to the length of \$word)\n"; $has_unique++; } print "\n"; } print "I print the whole list: ",(join ' ', @$list),"\n\n" if $has +_unique; } # output got list [allo mallo malo] analizing word [allo] Got char [a] Got char [l] Got char [l] Got char [o] chars count is: l = 2 a = 1 o = 1 analizing word [mallo] Got char [m] Got char [a] Got char [l] Got char [l] Got char [o] chars count is: l = 2 a = 1 m = 1 o = 1 analizing word [malo] Got char [m] Got char [a] Got char [l] Got char [o] chars count is: l = 1 a = 1 m = 1 o = 1 malo has no repeated letters (keys of %chars are equal to the +length of $word) I print the whole list: allo mallo malo ...
      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found