karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

This is something it i would like to share because the technique was so often mentioned on PM and never really explained as far as i can remember.

By chance i stumbled over List::Gen which provides slide {CODE} WINDOW LIST.

Unfortunately the module doesn't compile on my system/with my Perl version.

But it seems to be quite easy to steal some code from it that seems to work like a charm:

#!/usr/bin/env perl use strict; use warnings; use List::Util::PP qw(min reduce); use feature qw(say); sub slide (&$@); say slide {qq(@_\n)} 2 => 1 .. 4; say slide {uc qq(@_\n)} 2 => qw(nose cuke foo bar); sub slide (&$@) { my ($code, $window) = splice @_, 0, 2; $window--; map $code->(@_[$_ .. min $_+$window, $#_]) => 0 .. $#_; } __END__ Karls-Mac-mini:Desktop karl$ 1 2 2 3 3 4 4 NOSE CUKE CUKE FOO FOO BAR BAR

To me this looks like it works as designed and expected.

Thanks for any hint and comment. Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Replies are listed 'Best First'.
Re: Sliding window revisited: Is it OK to make it so?
by choroba (Cardinal) on Jul 19, 2019 at 21:36 UTC
    Seems OK. Cf. Matching in huge files.
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      "...OK..."

      Thanks for absolution and especially for the hint in the CB. See also my reply to LanX below. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Sliding window revisited: Is it OK to make it so?
by LanX (Saint) on Jul 19, 2019 at 22:11 UTC

      Thanks LanX. I knew the links that you and choroba provided. But the advice given ibidem seemed to me like an endless loop. Probably yet another algorithm that I didn’t understand for a long time - or may be i got this notorious mental block again. But I begin to see the things more clearly now. And i guess that i need to read again some basic manpages to understand my stolen solution 🤪. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        Sorry I have trouble understanding all of this, that's why I gave a generic hint

        The example code you showed seems to slide over a fixed array, but when dealing with huge files you need a dynamic approach.

        Otherwise you wouldn't get the file into memory.

        Unless there is "lazy" magic connected to the array via tie*, but this must be in other parts of the module, which I never used

        In short, I'm confused what the question is and how to help.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        *) Yep seems so https://metacpan.org/pod/List::Gen#generators

Re: Sliding window revisited: Is it OK to make it so?
by hippo (Archbishop) on Jul 20, 2019 at 09:04 UTC
    Unfortunately the module doesn't compile on my system/with my Perl version.

    The test matrix suggests that it fails on any modern perl > 5.19.3. That's a shame as it seems like it would otherwise be a useful module. There are some open tickets so maybe you can recognise the failure you see in one of those?

      "...a shame..."

      Yes, indeed. But i‘m glad that i found some working implementation that I can comprehend. And it seems to be sound. Please see above for my struggle with this technique. Thanks and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Sliding window revisited: Is it OK to make it so?
by LanX (Saint) on Jul 20, 2019 at 15:31 UTC
    It's totally unclear to me which kind of problem you are trying to solve with the sliding window.

    Or if you are just interested to play around with iterators.

    Point is that realizing a solution to the example I gave, i.e. to search a huge file with a regex doesn't seem trivial with the module you chose.

    The documentation mentions a file iterator but it's operating on record separators not on fixed size chunks.

    Hence you'd need to generate a chunk iterator, which masks as a tied "lazy" array.

    But do you really need to solve THIS problem?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice