DEV Community

Kian Meng, Ang
Kian Meng, Ang

Posted on • Updated on

Perl Weekly Challenge 034

Originally published in kianmeng.org and Perl Weekly Challenge.

Continues from previous week.

Feel free to submit a merge request or open a ticket if you found any issues with this post. We highly appreciate and welcome your feedback.

For a quick overview, go through the original tasks and recap of the weekly challenge.

Additional feedback to the our Perl Weekly Challenge’s Twitter account is much appreciated.

Task #1

CPAN modules used: Data::Dumper, Getopt::Long, List::Util, Modern::Perl, PDL, PDL::Core, PDL::IO::Image, PDL::ImageRGB, PDL::NiceSlice, feature, strict, warnings

While this may seem like a simple task, it's a good example of showing how Perl handles slicing for an Array or Hash. From all the submissions, we can group the solution into two categories of showing how slicing works in Perl language itself or through a practical example.

For the first category, start with the solution by Mark Anderson, Javier Luque, Adam Russell, Laurent Rosenfeld, and E. Choroba. Next, look into the submission by Ruben Westerberg and Steven Wilson where both demonstrated array and hash slicing using different range of values and edge cases.

For the second category, look into different examples that illustrated the usage of slicing array and/or hash. These examples include image processing by Burkhard Nickels, binary search by Jaldhar H. Vyas, superheroes names by Andrezgz, prime numbers generator by Colin Crain, sovereign states by Petr Roubicek, card shuffling by Duane Powell, and user listing by Dave Cross.

And kudos given to Dave Cross for his solution that demonstrated both list and hash slicing in one line of code as shown below.

    # Lvalue is a hash slice
    # Rvalue is a list slice
    @user{qw[user id shell]} = (split /:/)[0, 2, -1];

Lastly, for good write-up on this topic, read the comment by Colin Crain and the details blog post by both E. Choroba and Laurent Rosenfeld.

Task #2

CPAN modules used: Data::Dumper, Getopt::Long, PDL, PDL::Core, PDL::IO::Image, PDL::ImageRGB, PDL::NiceSlice, feature, strict, warnings

In the past challenges, we've discussed about Dispatch table, which is just a table with pointers to function or method. Implementation in Perl is rather easy, just a hash to function pointers.

For a quick understanding on how it being used in Perl, start with the submission by Adam Russell, Ruben Westerberg, Steven Wilson and E. Choroba.

For more complex practical examples of Dispatch table implementation, look into the solution by Dave Cross (console calculator), Javier Luque (calculator), Duane Powell (card shuffling), Burkhard Nickels (console command), Laurent Rosenfeld (dictionary), Jaldhar H. Vyas (Rock paper scissors game), Roger Bell West (expression evaluator), Andrezgz (superheroes name), and Colin Crain (another console calculator, yes this was a popular choice among participants).

See Also

(1) Perl Weekly Challenge - 034 by Javier Luque
(2) Sliced Dispatch, The Raku Way by Arne Sommer
(3) Perl Weekly Challenge 034 by Adam Russell
(4) Perl Weekly Challenge #34 by Burkhard Nickels
(5) Perl Weekly Challenge 34: Array and Hash Slices and Dispatch Tables by Laurent Rosenfeld
(6) Perl Weekly Challenge: Week 34 by Jaldhar H. Vyas
(7) Perl Weekly Challenge 034: Slices and a Dispatch Table by E. Choroba

Top comments (0)