in reply to Re: Perl script to match a regexp in the prior line and other multiple lines
in thread Perl script to match a regexp in the prior line and other multiple lines

Hi tybalt89 ,

Thanks for your help! I have tried the script but there is one problem , I am getting the below stated errors ,

Bareword found where operator expected at script.pl line 13, near "tr//\0/cr"

Bareword found where operator expected at script.pl line 14, near "tr/\0//dr"

I am using perl version 5.10 and cant change it to the other latest version since I dont have administrator right Can we tweak the provided solution to make it work 5.10 ?

Thanks and Regards

Kshitij Kulshreshtha

  • Comment on Re^2: Perl script to match a regexp in the prior line and other multiple lines

Replies are listed 'Best First'.
Re^3: Perl script to match a regexp in the prior line and other multiple lines
by afoken (Chancellor) on Dec 15, 2019 at 17:04 UTC

    First, don't manually copy code from perlmonks, use the download link below the code. That prevents typos.

    Second, don't assume any code posted at perlmonks will solve your problem. Some people post untested or intentionally bad code, some of them to make you learn, some just don't know better, and some are simply assholes. tybalt89 has a long history of posting completely undocumented, completely unexplained code, often needlessly complex or obfuscated, often working only accidently.

    Third, perlmonks is not a code writing service. We are here to help you learn perl, not to write code for you. (And this is part of the motivation for people posting bad code.)

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^3: Perl script to match a regexp in the prior line and other multiple lines
by tybalt89 (Monsignor) on Dec 18, 2019 at 16:27 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11110110 use warnings; $_ = do { local $/; <DATA> }; print " pattern offset pin_num\n"; while( /\^/g ) { my $pre = $`; my ($pattern, $offset) = $pre =~ /^(\S+) +(\S+).*\n(.*)\z/m; my $skip = length $3; printf "%6s%7s%5s%s%s%9s\n", $pattern, $offset, ($pre =~ /^.{$skip}(\w)/gm)[ 0..2, -1] } __DATA__ p p p p p Pin Numbers 3 2 1 8 9 1 2 3 4 5 pattern offset scan1 2965 H L H L H ^ scan2 2200 L H H L H ^ scan3 1100 H L L L L ^ scan4 1500 L L H H H ^ scan5 2800 H H L H H ^ ^
Re^3: Perl script to match a regexp in the prior line and other multiple lines
by Anonymous Monk on Dec 15, 2019 at 09:44 UTC
    Can you tweak it?

      Can anyone tell ?

      What would be equivalent of the below stated code in the perl 5.10

      tr//\0/cr"

      Thanks Kshitij

        The  /r modifier was added to  tr/// s/// operators in Perl version 5.14: see Quote Like Operators for  tr/// and Regexp Quote Like Operators for  s/// both in perlop.

        Try the following untested changes:
            my $mask = $3 =~ tr//\0/cr . "\xff";
        to
            (my $mask = $3) =~ tr//\0/c;
            $mask .= "\xff";
        and
            my $column = join '', map +($_ & $mask) =~ tr/\0//dr, split /^/, $pre;
        to
            my $column = join '', map { (my $r = $_ & $mask) =~ tr/\0//d;  $r; } split /^/, $pre;

        (And yes, heed the advice of afoken here; tybalt89 is the Genie of the Regex, and centuries of folk wisdom tell us that deals done with such a being seldom end well. :)


        Give a man a fish:  <%-{-{-{-<