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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay