We don't bite newbies here... much PerlMonks

User Posts

 Need Help??
Posts by Anonymous Monk
Fast sliding submatrix sums with PDL (inspired by PWC 248 task 2) in Meditations
5 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 25, 2023 at 19:51
```Task 2: Submatrix Sum
Submitted by: Jorg Sommrey

You are given a NxM matrix A of integers.

Write a script to construct a (N-1)x(M-1) matrix B
having elements that are the sum over the 2x2 submatrices of A,

b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1]

Example 1

Input: \$a = [
[1,  2,  3,  4],
[5,  6,  7,  8],
[9, 10, 11, 12]
]

Output: \$b = [
[14, 18, 22],
[30, 34, 38]
]

I tried to crank it up for entertainment -- to solve not "2x2", but arbitrary "WxH" sub-matrices of (relatively) large matrices. Sadly, it was too late into many stupid plots and tables, when I realized, that excessive summation isn't required at all. Sliding (moving) sum (or average, min/max, etc.) is a well known concept -- duh! not to un-educated me, alas. And so maybe "2x2", not "WxH", in the task, was selected not only because PWC tries to accommodate new learners. Anyway, I implemented "sliding submatrix sums" algorithm only at a later stage, in PDL, and didn't "backport" it to pure Perl (nor Pdlpp) because had already been somewhat fed-up with this 248-2, but decided to present sanitized results as a meditation.

PWC 244 task 2 in linear time in Meditations
3 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 27, 2023 at 13:22

Disclaimer: it's clickbait. The plot is curved, solution isn't linear, despite lack of nested loops, -- but fast.

```Task 2: Group Hero

You are given an array of integers representing the strength.

Write a script to return the sum of the powers of all possible
combinations; power is defined as the square of the largest number
in a sequence, multiplied by the smallest.

Example 1

Input: @nums = (2, 1, 4)
Output: 141

Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8
Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1
Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64
Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4
Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32
Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16
Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16

Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141
```
TPF PERL Swag in Meditations
by Anonymous Monk
on Jan 09, 2023 at 02:42
I want some swag from The Perl Foundation but the graphics...

1. They spelled it: PERL
2. Raptors are extinct.
3. Raw onions? C'mon!

I want cool cryptic, like Perl itself:

\$Perl->{5.36}

 Front Back #!/usr/bin/perl exit; BEGIN{} END{} __DATA__ use Perl; \$? \$@% ... \$_ @_

Obviously: camel code

Perl::Dist::APPerl - Actually Portable Perl in Perl News
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 06, 2023 at 07:34
"Actually Portable Perl (APPerl) is a distribution of Perl that runs on several x86_64 operating systems (most Unix-like and Windows) via the same binary. It builds to a single binary with perl modules packed inside of it. Cross-platform, single binary, standalone Perl applications can be made by building custom versions of APPerl, with and without compiling Perl from scratch"

• Perl::Dist::APPerl
• computoid.com/APPerl/
Double the speed of Imager->setpixel in Meditations
3 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 11, 2022 at 16:49
My app was taking 5 seconds to generate an image involving about a million calls to Imager->setpixel and this felt way too slow. NYTProf revealed the cause to be expensive sanity checking in Imager.pm at lines 3456 and 3465. Commenting those lines gets me down to 3 seconds and this now feels much less slow:
```sub setpixel {
my (\$self, %opts) = @_;

#  \$self->_valid_image("setpixel") or return;

my \$color = \$opts{color};
unless (defined \$color) {
\$color = \$self->{fg};
defined \$color or \$color = NC(255, 255, 255);
}

#  unless (ref \$color && UNIVERSAL::isa(\$color, "Imager::Color")) {
#    unless (\$color = _color(\$color, 'setpixel')) {
#      \$self->_set_error("setpixel: " . Imager->errstr);
#      return;
#    }
#  }

unless (exists \$opts{'x'} && exists \$opts{'y'}) {
\$self->_set_error('setpixel: missing x or y parameter');
return;
}
...
}

sub _valid_image {
my (\$self, \$method) = @_;

ref \$self
or return Imager->_set_error("\$method needs an image object");

\$self->{IMG} && Scalar::Util::blessed(\$self->{IMG}) and return 1;

my \$msg = \$self->{IMG} ? "images do not cross threads" : "empty inpu
+t image";
\$msg = "\$method: \$msg" if \$method;
\$self->_set_error(\$msg);

return;
}
Should the next version of Imager have an option to disable global sanity so it can operate almost twice the usual speed?
The new black metacpan (meta::cpan throws away brand) in Meditations
by Anonymous Monk
on Sep 30, 2022 at 06:15
the distribution is barmy in Meditations
by Anonymous Monk
on May 21, 2021 at 09:36
put back the commented away 63 71 and 75 and it looks as we expect(?) - a file of random numbers from the physics wizards is referenced too - put in 27 remove 28
```use strict;
use warnings;
use List::Util qw(max sample);
#use lib '/usr/local/share/perl/5.30.0';
#use Statistics::Basic::Stddev; #installs somewhere??
#use Statistics::Basic::StdDev;
use Statistics::Basic qw(:all);
use File::Slurp; #for large list of real randoms

my (\$fertilewomen) = @ARGV;
my @maxxes;
my \$cyclelength = 32;
#my \$duration = int(rand(1) + 0.5) == 1 ? 4 : 5;
#my \$duration = sample 1, (4, 5);
\$fertilewomen ||= 48;
#print "duration is \$duration\n";
#my @women;

#for (1..100) {
my @women;

#    for (1..48) {
for (1..\$fertilewomen) {
#        my \$start = int(rand(31) + 0.5);
#        my \$start = int(rand(\$cyclelength-1) + 0.5);
#        my \$start = int(rand(\$cyclelength));
my \$start = getrandom(\$_);
#    print "\$start\n";
#        my \$range;
#        my \$turnover = \$cyclelength - \$duration;
#        if (\$start > 28) {
#        if (\$start > \$turnover) {
#            if (\$start == 29) {
#            if (\$start == \$turnover+1) {
#                \$range = [\$start, \$start+1,  \$start+2, 0];
#            } elsif (\$start == 30) {
#            } elsif (\$start == \$turnover+2) {
#                \$range = [\$start, \$start+1,  0, 1];
#            } else {
#                \$range = [\$start, 0, 1, 2];
#            }
#        } else {
#            \$range = [\$start..\$start+3];
#        }
#        push @women, \$range;
#        my \$duration = sample 1, (4, 5);
my \$duration = 4;
push @women, \$start..(\$start+\$duration-1);
}

my %count;
my %mcount;

for my \$woman (@women) {
#        my @range = @{\$woman};
#        for (@range) {
#            my \$moduluscyclelength = \$_ % \$cyclelength;
my \$moduluscyclelength = (\$woman % \$cyclelength);
#print \$moduluscyclelength, ' ', \$woman, ' ', \$cyclelength, "\n";
#            \$count{\$_}++;
\$mcount{\$moduluscyclelength}++;
#        \$count{\$woman}++;
#        }
}

my %occupancy;

#    for (0..31) {
for (0..\$cyclelength-1) {
#        my \$value = exists \$count{\$_} ? \$count{\$_} : 0;
my \$mvalue = exists \$mcount{\$_} ? \$mcount{\$_} : 0;
#        \$occupancy{\$_} = \$value;
print "\$_ \$mvalue\n";
#        print "\$_ \$value \$mvalue\n";
}

#    print 'mean is ', mean(values %count), "\n";
#    print 'sd is ', stddev(values %count), "\n";
#    my %ostats;

#    for (values %occupancy) {
#        \$ostats{\$_}++;
#    }

#    for (sort { \$a <=> \$b } keys %ostats) {
##        print \$_, ' ', \$ostats{\$_}, "\n";
#    }

#    push @maxxes, max(keys %ostats);
#}

#print join ' ', sort { \$a <=> \$b } @maxxes;

sub getrandom {
my \$index = shift;
my \$byte = \$rands[\$index];
chomp \$byte;
return int(\$byte/8);
}
When Perl saved the day (and Python couldn't) in Meditations
3 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 28, 2020 at 15:59

Yes, I know this is not a place to rant, nor is this place to talk bad about other languages, but heck, I am going to rant anyways....

So I started my scripting journey (short and irregular) with Perl. It enabled me to write scripts pretty fast with least knowledge of the language in particular and programming in general. Job requirements made me take a long break from Perl and Scripting. I started learning Python, not because I had to write scripts, but to "check mark" a training requirement. It's a good language, nothing against it. I was happy on the learning journey, until a requirement came up to automate some alerts and that is when my troubles started.

The client for which I was to do the automation has strict internet access policies, so no access to the internet from the servers used for environment management. Also, no admin access. I had promised to automate some stuff (my bad, I should have first checked the requirements). Python would not install, not even within the directories for my account. I was at wit's end. I tried downloading again thinking may be the file got corrupted, but same result. My colleague managed to install it for his servers (different client) but kept getting errors while trying to send out emails. So we both were kind of upset and pissed off.

And then I remembered, Strawberry Perl. Downloaded it, pushed the installer to the servers (Yes, I again had to raise a request for that as well, and no they would not allow any special permissions). To my amazement, it installed without any errors!!. And every conceivable module I wanted was there!!

This was two days ago...just now finished writing the scripts, sent a test email to myself, then sent it to the required DL Email. Informed my manager and the concerned client folks and they were all happy!!

Now for my colleague, he too installed the same, I helped him out with the scripts, and I dunnowhathappened, but the emails just worked with Perl. Yeah this sounds stupid, but it didnt work with Python, but , it worked with Perl!!

Also I found that, for Perl, there are two kind of Email Modules, Ones that "create"/"format" Email and the others you use to "send" emails. Some modules may have both, and I could be wrong, but what I really liked is the modularity given here.

May be I did something wrong while trying to install Python, may be not, I am not sure. May be I was stupid. But, end of the day, it was Perl that saved the day.

So thank you Perl, Thank you Perl Developers and especially Strawberry Perl Team for saving the day.

Rant Over.

IntraMine service suite in Perl News
by Anonymous Monk
on May 26, 2020 at 13:59

Dear suPerlatives,

Allow me to introduce Intramine, an intranet service suite for Windows done in Strawberry Perl and JavaScript that provides sub-second local search of your half a million or more source and text files, among other things.

Some other things:

• five-second index update when you change a file, to keep searches current
• automatic linking for all source and text and image file mentions, with minimal overhead (often none)
• a really nice file Viewer to browse your files, and see search hits in full context (plus that automatic linking)
• image hovers in your source and text files
• Gloss, a markdown variant specifically for intranet use that takes advantage of autolinking and minimizes "computer friendly" overhead
• scalable services: write your own IntraMine service, with or without a front end, and run multiple instances that can talk to other services
• Search, Viewer, and Linker service support for 137 programming languages, as well as plain text
• all original work is covered by an UNLICENSE.

https://github.com/KLB7/IntraMine

Cheers,

KLB7

at intramine.info

Perl joke heard on television in Meditations
3 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 28, 2020 at 07:41
Stacy Herbert: You know the flu is way more complicated than this corona virus. I think it's like four strands of RNA. It's so simple apparently the code for it fits on one single page, and this simple little tiny virus is taking down our hyper-complex globalized just-in-time system.

Max Keiser: Yeah I think the COVID-19 is written in Perl, and the flu is written in C++.

```Keiser Report E1520 Gold: Problems with Exchange for Physical
```

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-01 23:40 GMT
Voting Booth?
My favourite way to spend a leap day ...

Results (29 votes). Check out past polls.