DEV Community

oldtechaa
oldtechaa

Posted on

Perl Weekly Challenge #220 - I've Seen These Characters 'Round These Parts

Hi everybody, just a quick one this week. Again it's been a very busy week, so I wrote this one quick to print the sorted list of all common characters in all the words provided. That's the simple explanation of this week's challenge.

Here's the code:

my @results;
my $first_word = shift;
for my $letter (split(//, $first_word)) {
    push(@results, lc($letter)) if (grep {$_ =~ /$letter/i} @ARGV) == @ARGV;
}
@results = sort @results;
say $_ foreach @results;
Enter fullscreen mode Exit fullscreen mode

So, for how it works. We take the first word, split it up into its letters, and look through it. All common characters have to appear in the first word, so we can just limit ourselves to those letters. We loop through each letter in the word, and do a regex grep to find any words containing the letter. If every word matches, we add the letter to the resulting list, then sort and print each one.

I got to learn more about grep this week, so that will help me be more idiomatic with it.

Stay safe and I'll hopefully see you next week!

Top comments (0)