📘 Anagram test using Perl 6

Tell if the two words are anagrams of each other.

Anagrams are words or phrases that are built out of the same letters. We start with checking words only.

my $a = prompt('First word > ');
my $b = prompt('Second word > ');

say normalize($a) eq normalize($b) 
    ?? 'Anagrams.' !! 'Not anagrams.';

sub normalize($word) {
    return $word.split('').sort.join('');

The words, stored in the $a and $b variables, are passed through the normalize function, which converts a word into a string, where all the letters are alphabetically sorted. For example, the ‘hello’ string becomes ‘ehllo’. If both words can be normalised to the same form, they are anagrams.

To make the program accept phrases, let’s modify the normalize function so that it removes the spaces from the phrase and makes all the letters lowercase:

sub normalize($word) {
    return $word.lc.split('').sort.join('').trans(' ' => '');

There are two additions to the above chain of method calls: lc converts the string to the lowercase version, and the trans method replaces all the spaces with an empty string. After these changes, the ‘Hello World’ phrase becomes ‘dehllloorw’.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s