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

#!/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+(\S+).*\n(.*)\z/m; my $mask = $3 =~ tr//\0/cr . "\xff"; my $column = join '', map +($_ & $mask) =~ tr/\0//dr, split /^/, $pr +e; my ($pin, $hl) = $column =~ /(...).*(.)/; printf "%6s%7s%7s%9s\n", $pattern, $offset, $pin, $hl; } __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 ^ ^
  • Comment on Re: Perl script to match a regexp in the prior line and other multiple lines
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl script to match a regexp in the prior line and other multiple lines
by kshitij (Sexton) on Dec 15, 2019 at 09:41 UTC

    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

      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". ;-)
      #!/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 ^ ^
      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