## Date Finder

Create a script to accept a 7 digits number, where the first number can only be 1 or 2. The second and third digits can be anything 0-9. The fourth and fifth digits corresponds to the month i.e. 01, 02, 03…, 11, 12. And the last 2 digits respresents the days in the month i.e. 01, 02, 03…, 29, 30, 31. Your script should validate if the given number is valid as per the rule and then convert into human readable format date.

RULES

1. If 1st digit is 1, then prepend 20 otherwise 19 to the 2nd and 3rd digits to make it 4-digits year.
2. The 4th and 5th digits together should be a valid month.
3. The 6th and 7th digits together should be a valid day for the above month.

For example, the given number is 2230120, it should print 1923-01-20.

As we’ve done several times, we’ll use the core module Time::Piece to handle dates.

``````#!/usr/bin/perl
use warnings;
use strict;

use Time::Piece;

sub validate {
my (\$number) = @_;``````

First, we’ll check the length of the input string.

``    die 'Invalid length' unless length \$number == 7;``

## Weekdays

Write a script to calculate the total number of weekdays (Mon-Fri) in each month of the year 2019.

I used the core module Time::Piece and its companion from the same distribution, Time::Seconds. Let’s start on the first day of the month, and keep adding one day while we stay in the same month. Along the way, count the days that aren’t Saturdays and Sundays.

``````#!/usr/bin/perl
use warnings;
use strict;

use Time::Piece;
use Time::Seconds qw{ ONE_DAY };

sub days_in_month {
my (\$month) = @_;
my \$date = 'Time::Piece'->strptime("2019 \$month 1 12:00",
'%Y %b %d %H:%M');
my \$count = 0;
while (\$date->month eq \$month) {
++\$count unless grep \$date->day eq \$_, qw( Sat Sun );
\$date += ONE_DAY;
}
return \$count
}``````

And here’s a test that the numbers are correct:

## VIN Validation

Write a program to validate given Vehicle Identification Number (VIN).

I followed the description at Wikipedia. Sometimes, it wasn’t exactly clear whether the described rule should be valid everywhere or just in a part of the world; the rules also developed with time, so older vehicles can bear VINs that would be considered invalid for a modern car.

Most of the validation is implemented in a single subroutine `validate_vin`. It takes two parameters, `\$vin` and `\$sold`: the second one says where the car was sold. `"North America"` and `"China"` are two values that trigger a different behaviour of the validator.

## The Encoder

Write a program to encode text into binary encoded Morse code.

Before we can encode Morse code into its binary representation, we need to encode normal text into Morse code. As a former Woodcraft member, I was able to write the following lines by heart:

``````my %to_morse = qw( a .-   b -... c -.-. d -..  e .    f ..-. g --.
h .... i ..   j .--- k -.-  l .-.. m --   n -.
o ---  p .--. q --.- r .-.  s ...  t -    u ..-
v ...- w .--  x -..- y -.-- z --.. );``````

The encoding subroutine is straightforward: split each word into separate characters, then replace each with the value from the above hash.

``````sub encode_to_morse {
join '/', map \$to_morse{\$_} // "", split //, shift
}``````

Note that space is not present in the translation table, so it gets translated to an empty string, which creates the expected double slashes between words.

## Slices

Write a program that demonstrates using hash slices and/or array slices.

In the spirit of TIMTOWTDI I decided to write a single program that demonstrates both the tasks at the same time.

Let’s start with slices. Slices are parts of structures (arrays and hashes). Slicing has a special syntax by which you tell Perl which indices or keys you want to use to obtain a slice.

For example, consider the following array:

``````my @keys = qw( not_this_one
this_one
this_one_too
it_was_enough );``````

Naturally, we want to select the second and third one. We can use

``\$keys, \$keys``

or

``map \$keys[\$_], 1, 2;``

but there’s a shorter and cleaner syntax for the same:

``@keys[1, 2]`` I blog about Perl.