Perl Weekly Challenge 187: Days Together and Magical Triplets

These are some answers to the Week 187 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Task 1: Days Together

Two friends, Foo and Bar gone on holidays separately to the same city. You are given their schedule i.e. start date and end date.

To keep the task simple, the date is in the form DD-MM and all dates belong to the same calendar year i.e. between 01-01 and 31-12. Also the year is non-leap year and both dates are inclusive.

Write a script to find out for the given schedule, how many days they spent together in the city, if at all.

Example 1

Input: Foo => SD: '12-01' ED: '20-01'
       Bar => SD: '15-01' ED: '18-01'

Output: 4 days

Example 2

My Favorite Modules: Term::ReadLine::Perl

Term::ReadLine is a core module (since Perl 5.002) that provides an extremely limited text interface of the prompt-and-type variety. Its main virtue is that you can add a back end which gives it things like command history, editing, and completion.

The back ends live in the Term::ReadLine::* name space, and you can control which one you get by defining the PERL_RL environment variable as documented at Term::ReadLine. If this is not defined, various undocumented things are tried; if none works you get the bundled minimal interface, Term::ReadLine::Stub.

The preferred back end (at least, according to Bundle::CPAN as of this writing) is Term::ReadLine::Perl. This is a shy, retiring module, with no POD documentation at all, which provides readline-style history, editing, and completion. By default the only completion you get is file name completion, but with some work you can expand this to do whatever you can figure out.

At this point, some words of caution are probably in order.

Some Perl Code In Memory of a Great Scientist

On August 21, 2021, famous Polish mathematician Andrzej Schinzel passed away at the age of 84. He was one of the great minds behind modern number theory. May he rest in peace. I have extended one of my CPAN modules relating to his work and dedicated the release to his memory.

I try match syntax using Syntax::Keyword::Match


Syntax::Keyword::Match is a module to enable match syntax in the current Perl by Paul Evans who is one of the current Perl Steering Councils. See perlgov about the Perl Steering Council.

Syntax::Keyword::Match Examples

Syntax::Keyword::Match Examples

Number matching

Number matching. Match syntax is similar as a switch syntax of C language.

Perl Weekly Challenge 186: Zip List and Unicode Makeover

These are some answers to the Week 186 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on Oct. 16, 2022 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Zip List

You are given two lists @a and @b of same size.

Create a subroutine sub zip(@a, @b) that merges the two lists as shown in the example below.

Example:

Input:  @a = qw/1 2 3/; @b = qw/a b c/;
Output: zip(@a, @b) should return qw/1 a 2 b 3 c/;
        zip(@b, @a) should return qw/a 1 b 2 c 3/;

Zip List in Raku

Monthly Report - September

Finally enjoying again ...

The month of September is very special to me personaly.

Why?

Well, I got married in the very same month 18 years ago. The best part is, I choose the day 11 to get married. I have never missed my wedding anniversary, thanks to all the TV news channel.

How?

On the day, every year I find every TV news channel talk about 9/11 episode. It works like reminder to me.

Let's get back to the main topic ...

For the last few months, I have been late in making monthly report on time. This has created many problems for me. One of them and the most important is that I don't remember what I did last month. I then look back my activities on various social platforms that I am active on e.g. Facebook, Twitter and LinkedIn. It doesn't always help as I am not very active either socially these days.

BTW, I started working on this report around 6 am today.

So what's going on?

My Favorite Warnings: uninitialized

This warning was touched on in A Belated Introduction, but I thought it deserved its own entry.

When a Perl scalar comes into being, be it an actual scalar variable or an array or hash entry, its value is undef. Now, the results of operating on an undef value are perfectly well-defined: in a nuneric context it is 0, in a string context it is '', and in a Boolean context it is false.

The thing is, if you actually operate on such a value, did you mean to do it, or did you forget to initialize something, or initialize the wrong thing, or operate on the wrong thing? Because of the latter possibilities Perl will warn about such operations if the uninitialized warning is enabled.

If you really intended to do this, no warnings 'uninitialized'; will suppress the error.

Web::PageMeta - a mixed sync/async lazy Perl Moose HTTP-GET module

Announcing here Web::PageMeta which is lazy build-ed HTTP-GET and web-scrape-data module able to work both in classic code and also to behave non-blocking in async code. More info on my blog or on CPAN or on GitHub .

Perl Weekly Challenge 185: MAC Address and Mask Code

These are some answers to the Week 185 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Task 1: MAC Address

You are given MAC address in the form i.e. hhhh.hhhh.hhhh.

Write a script to convert the address in the form hh:hh:hh:hh:hh:hh.

Example 1:

Input:  1ac2.34f0.b1c2
Output: 1a:c2:34:f0:b1:c2

Example 2:

Input:  abc1.20f1.345a
Output: ab:c1:20:f1:34:5a

MAC Address in Raku

This is done in a hurry, less than 45 minutes before the deadline. There might be a better or simpler way to solve this task, but using a couple of regexes is so simple that I don’t see any reason to try something else.

Latest Perl Introduction 2021 Movie

Perl version 5.36. isa operator. try catch syntax. enable warnings. use v5.36. use v7. The introduction of the members of Perl core team.

My Favorite Modules: if

My blog post My Favorite Warnings: redundant and missing touched on the use of the if module. Comments on that post made me think it deserved a top-level treatment, expanding on (though not necessarily improving on) Aristotle's comment.

The if module is a way to conditionally load another module and call its import() or unimport() method. Sample usages are

use if CONDITION, MODULE, ...; # load and import
no if CONDITION, MODULE, ...;  # load and unimport

In actual use the CONDITION is an expression that is evaluated at compile time. If that expression is true the named module will be loaded and its import() or unimport() method called with whatever arguments were specified. If the condition is false, nothing is done. If MODULE is a string literal it will have to be quoted, since as far as Perl is concerned it is just another argument.

Util::H2O ~ Iterative Refinement of Existing Perl Code

Util::H2O is an incredibly powerful tool for managing HASH references in a more natural way.

This post is the first of several that will explore this awesome module. I've started using it quite a bit in both new code and in existing code. There are several imporant cases where it really shines. Here we explore the power it has to iteratively refine existing code. It's also fun and easy to introduce into existing code.

Util::H2O provides a method called h2o that provides a very powerful way for turning a hash reference to an object . Generally speaking, this means I get accessors with as few keystrokes as possible.

I've been using this module quite a bit recently, and I really do like the improvements it has over the more traditional modules used for generating accessors.

The most basic use of h2o is to provide accessors to a hash reference with a single level of keys. I tend to use hash references a lot .

Perl Weekly Challenge 184: Sequence Number and Split Array

These are some answers to the Week 184 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on Oct. 2, 2022 at 23:59). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Sequence Number

You are given list of strings in the format aa9999 i.e. first 2 characters can be anything ‘a-z’ followed by 4 digits ‘0-9’.

Write a script to replace the first two characters with sequence starting with ‘00’, ‘01’, ‘02’ etc.

Example 1

Input: @list = ( 'ab1234', 'cd5678', 'ef1342')
Output: ('001234', '015678', '021342')

Example 2

Input: @list = ( 'pq1122', 'rs3334')
Output: ('001122', '013334')

Sequence Number in Raku

SPVM 0.9014 Release - add class, method, static keyword, omit SPVM:: namespace

I release SPVM 0.9014. Latest releases have some big changes.

add class, method, static keyword, omit SPVM:: namespace, and remove sub, self, keyword.

Before

Monthly Report - August

Finally enjoying again ...

Ever since I joined Oleeo, I keep talking about it in every monthly report.

Why?

Well, right from day one, I have been getting to work on something I never worked on before. To be honest with you, I was expecting to fight with good old CGI ridden code mostly. I find myself lucky to have such a great supporting team. Right now I am playing with Elastic Search and I am enjoying it. Thanks to CPAN for such a cool library, Search::Elasticsearch.

Did you notice last monthly report was published on 22nd Aug?

I have never been so late ever since I started the series of monthly report.

You must be thinking, why bother with monthly report? Who cares what I do?

I agree, nobody cares. But I still do it every month since Nov 2018, my first monthly report was published on 2nd Nov 2018. In two months time, I would complete 3 years of monthly reporting. Honestly speaking, I didn't realise it until now.

My Favorite Warnings: redundant and missing

The redundant and missing warnings were added in Perl 5.22 to cover the case where a call to the printf or sprintf had more (redundant) or fewer (missing) arguments than the format calls for. The documentation says that they may be extended to other built-ins (pack and unpack being named specifically) but as of Perl 5.34.0 only the printf() built-ins are covered.

I have (very occasionally) found myself writing a subroutine taking a printf-style format and some arguments, and letting the format specify which (if any) of the arguments actually appear in the output. If I just throw all the arguments after the format into the printf(), one of these warnings is very likely to be thrown, starting with 5.22, since use warnings; enables them by default.

Perl Weekly Challenge 182: Unique Array and Date Difference

These are some answers to the Week 183 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Task 1: Unique Array

You are given list of arrayrefs.

Write a script to remove the duplicate arrayrefs from the given list.

Example 1

Input: @list = ([1,2], [3,4], [5,6], [1,2])
Output: ([1,2], [3,4], [5,6])

Example 2

Input: @list = ([9,1], [3,7], [2,5], [2,5])
Output: ([9, 1], [3,7], [2,5])

Unique Array in Raku

The Raku solution is essentially a one-liner (more than one line because of the tests). We convert the sub-arrays into strings and use the unique built-in routine to remove duplicates.

How to show UTF-8 at the Windows command prompt

If you windows Perl user, It is good to know How to show UTF-8 at the Windows command prompt .

How to show UTF-8 at the Windows command prompt

One liner is yet buggy, however UTF-8 showing is good in Windows command prompt.

TWC 127: Intersection on a Sunday Afternoon

This is my entry for

The Weekly Challenge, week 127

Task 1, "Disjoint Sets" was basically something I've done before somewhere else. In fact, what I'm using is overkill for just determining if two sets intersect. I imagine most people would probably use the FAQ answer. However, I'm a fan of what cardinal LanX of Perl Monks fame was trying to do in making set intersection a more "organic" operation. I don't know how much those ideas developed, however, so I'll be looking at the other solutions to see if there's anything new.

I actually did use my perlmonks code on real problem a few years ago, in modified form. It does the trick pretty quickly compared to other approaches. Thanks perl hashing!

You can find my code for Task #1 here.

My Favorite Modules: diagnostics

One of the things the Perl 5 Porters work hard on is issuing diagnostics that are actually diagnostic. I think they do a pretty good job at this, but sometimes I need a bit more explanation than the typical one-line message.

Now, there is documentation on all of these in perldiag, but paging through that looking for my message is a pain.

Fortunately, there is a module for that: diagnostics. This module causes diagnostics to be expanded into their full explanation as it appears in perldiag.

Typically you would not put a use diagnostics; in your Perl code, though of course you could. Instead, you would load it via a command-line option to perl, or maybe via environment variable PERL5OPT. That is, either

$ perl -Mdiagnostics my-flaky-perl.PL

or

$ env PERL5OPT=-Mdiagnostics perl my-flaky-perl.PL

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.