July 2023 Archives

No One Is Immune to Abuse

Once again I'm writing about a TPRC talk instead of a weekly challenge project, but that's because I feel it's a very important topic to be aware of both inside and outside our community as Perl programmers. Sawyer X gave a really great talk about the abuse he personally experienced as a member of the Perl community. I've never experienced abuse in this or any other technology community, but I have experienced abuse before in other contexts. Also, as a disclaimer, although I'm sure many have seen some of Sawyer's situation play out in public, I haven't, as I believe it happened during my extended break from Perl and development in general.

That said, to see someone as passionate as Sawyer is talk about the abuse he suffered and back it up with real-life examples is amazing, first of all, because of his bravery in talking about it, but it also disgusts me that people treat each other so horribly even when working on a shared goal. Please watch his talk and if you see yourself ever so slightly in what he's talking about, even just in supporting an abuser from the sidelines, please change. That's not what the Perl community needs. That will not help it grow.

Speaking of which, that's why I'm not cross-posting this to dev.to. Much as I want to share the idea behind this with those outside our community, I don't want Perl getting a reputation for abusive people. This is a prime example of how abuse can harm everyone, especially if it ruins the reputation of a community in the eyes of others.

In short, if you're abusive in how you interact with others, just stop. Please! If you're not and you care about people being abused, speak up and let those abusing know it's not OK. And a big thank you to all the good kind members of the community who care about those they work alongside.

Perl v5.36, Debian, and Ubuntu

I normally only write about the Perl Weekly Challenge/The Weekly Challenge, but today I have a couple things to say about recent Perl versions. I've been watching the videos from TPRC2023, and loving the talk by Paul Evans What's New in Perl v5.38. I like to have a minimum version in my code generally that I can rely on having all the features from. I think I previously ignored v5.36 because it wasn't in Debian yet (and I typically only use packaged Perl), but thanks to Paul's talk I've really enjoyed looking at the perldelta, where some really groundbreaking Modern Perl changes were made just in the use v5.36 syntax.

Well, now two things have happened. v5.36 is the oldest release still officially supported by the Perl team, and it's in Debian stable. This makes it a great candidate to be my new target version for most code!

Unfortunately, Ubuntu 22.04 (one of my servers) is still stuck on v5.34, and this makes me sad. 😥

In related news, the new object system in v5.38 and beyond looks pretty sweet! I'm excited. Can't wait to see what else gets implemented in the next couple versions.

Perl Weekly Challenge #225 - Words to the Max and Diff Sum

Hi all! Back this week with both solutions to The Weekly Challenge for once. We've got a word counting challenge and one that I really don't know how to explain. You have to see the challenge to understand it.

Max Words

So this challenge is just to tell us what the longest sentence in a set of sentences is. How many words does it have? A very simple easy solution can follow:

my $highest;
foreach (@ARGV) {
    my @words = split(/ /, $_);
    shift @words if !$words[0];
    $highest = scalar @words if scalar @words > $highest;
}
say $highest;

We take the input sentences (in quotes) and split them by spaces. Now, I tested and splitting by space puts an empty element at the start of the array, so if there is such an empty element we remove it with the shift. Then we check the number of words in this sentence and replace the existing champion if this one is the highest. That one's pretty simple and took just about 15 minutes to write and test.

Left Right Sum Diff

No idea what the actual practical purpose of this one is, but it's an interesting challenge and a lot simpler than I initally thought, although I realized in the middle there were some additional cases to handle from what I was thinking.

The goal is to split a list of integers into @left, made of (0, $ints[0], $ints[0] + $ints[1], etc.) and @right, made of 'etc., $ints[-2] + $ints[-1], $ints[-1], 0). Then we iterate through and for each pair we take the absolute difference between$left[$]and$right[$]`.

So here's the code:

my @ints = @ARGV;
my (@left, @right, @diff);
push @left, 0;
unshift @right, 0;
if ($#ints) {
    foreach (1..$#ints) {
        push @left, $left[$_ - 1] + $ints[$_ - 1];
        unshift @right, $right[-$_] + $ints[-$_];
    }
}
foreach (0..$#ints) {
    $diff[$_] = abs($left[$_] - $right[$_]);
}
say $_ for @diff;

First we put the zeros on each list. That's just in case we have a integer list like the second example, with just one number in the list. Then if we have additional integers we count and add numbers to the right side of the @left list and the left side of the @right list.

We loop through both completed lists and we compute the absolute difference, then print the list one-by-one. Voila! Again, not sure what the practical application is, but an interesting challenge. Many thanks to Mohammad and an enjoyable pair of challenges. I also appreciate being the June champion, so you'll hear more of my bio when it's released.

Have a great week! See you next week.

Perl Weekly Challenge #224 - Passing Notes

Hi everybody! Just doing one challenge again this week. Time limitations hold me back once again.

This week we're looking for the letters of a target word in a source word, and we're not allowed to use the same letter twice. Spoiler alert because it's only Wednesday and you still have the rest of the week to submit solutions if desired.

The easiest way to do this is with a dictionary hash initialized like so:

foreach (split //, $source) {$chars{$_}++}

Many people use map() to do this, but I'm not a big fan of map in many cases because I feel it makes code less readable.

This gives us the number of occurrences of each letter in the original word.

Then we iterate through the target word and look for (and remove) the letters in the dictionary:

foreach (split //, $target) {
    if ($chars{$_}) {
        $chars{$_}--;
    } else {
        say 'false' and exit;
    }
}
say 'true';

Obviously if the script did more than just this we'd need a success flag in there instead, but as it is we can just exit if we ever don't find the matching character in the dictionary.

Anyways, that's the solution for this week! I have a definite idea of how I'd solve the second challenge, I just don't have the time to implement it. I look forward to seeing the other solutions, because I'm sure they'll have more efficient ways to do it.

About oldtechaa

user-pic Just getting back into Perl programming. I have a personal project, SeekMIDI, a small graphical MIDI sequencer.