DEV Community

Simon Green
Simon Green

Posted on

1 1

Weekly Challenge 110

Challenge, My solutions

TASK #1 › Valid Phone Numbers

Task

You are given a text file. Write a script to display all valid phone numbers in the given text file.

My solution

Let's start with what is a valid phone number. I can tell you only the phone number starting with + would actually work from where I am. But for the task I guess this isn't really important.

If I was doing this outside the task, I would have used Path::Tiny to read the file. As regular readers would know, I prefer not to use modules that aren't part of core Perl in these challenges.

For this task, I simply read the file line-by-line and output a line if it matches the regular expression /^(?:\+[0-9]{2}|\([0-9]{2}\)|[0-9]{4}) [0-9]{10}$/. I use 0-9 as \d includes digits in other languages.

Example

» ./ch-1.pl input1.txt 
0044 1148820341
+44 1148820341
(44) 1148820341
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Transpose File

Task

You are given a text file. Write a script to transpose the contents of the given file.

My solution

This task didn't mention the format is CSV, although the example would indicate the input is a CSV file. Outside the challenge I would probably use Text::CSV as this correctly handles escaping of values with commas in them.

For this task I read the input file and create an array of arrays with the values found called @lines. I then loop through each column and use map { $_->[$col] // '' } @lines and the join method to display each row of the output.

The logical-defined-or // is used in case some rows do not have the same number of columns and will prevent undef warnings in the output (albeit it to STDERR).

Examples

» ./ch-2.pl input2.txt
name,Mohammad,Joe,Julie,Cristina
age,45,20,35,10
sex,m,m,f,f
Enter fullscreen mode Exit fullscreen mode

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay