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

E HI Guys , I am not new to Perl but I am getting an issue and not able to understand how to write a script for the below stated problem. Problem Description : I am having one file and I need to create a file with some pattern match as stated below Input File

PINS PPPPPPPPPPPPPPP DDDDDDDDDDDEEEE [[[[[[[[[[[[[[[[ 198765432105432 0]]]]]]]]]]]]]]] ] + + + + VecAddr RptCnt VmemAddr Cycle + ------------------------------- ------ -------- ----- ---------------- +---------------- Pat6_Scan_comp_at_speed_m:24791 1 6156500 25701 .......h........ +................ Pat6_Scan_comp_at_speed_m:24884 1 6156593 25794 .h....h......... +................ Pat6_Scan_comp_at_speed_m:24891 1 6156600 25801 ...h.....h...... +................ Pat6_Scan_comp_at_speed_m:26759 1 6158468 27689 ................ +................ Pat6_Scan_comp_at_speed_m:29196 1 6160905 30160 ................ +................ Pat6_Scan_comp_at_speed_m:30671 1 6162380 31650 ....h.h......... +................ Pat6_Scan_comp_at_speed_m:30982 1 6162691 31966 .....hl......... +................ Pat6_Scan_comp_at_speed_m:30985 1 6162694 31969 .....h.......... +................ Pat6_Scan_comp_at_speed_m:30986 1 6162695 31970 .....l.......... +................ Pat6_Scan_comp_at_speed_m:31276 1 6162985 32266 ......h......... +................ Pat6_Scan_comp_at_speed_m:31562 1 6163271 32557 ................ +................ Pat6_Scan_comp_at_speed_m:31563 1 6163272 32558 ................ +................ Pat6_Scan_comp_at_speed_m:31593 1 6163302 32588 .......h........ +................ Pat6_Scan_comp_at_speed_m:31594 1 6163303 32589 .......h........ +................ Pat6_Scan_comp_at_speed_m:31637 1 6163346 32632 ........l....... +................ Pat6_Scan_comp_at_speed_m:31639 1 6163348 32634 ........l....... +................ Pat6_Scan_comp_at_speed_m:31645 1 6163354 32640 ..h............. +................ Pat6_Scan_comp_at_speed_m:31646 1 6163355 32641 ..l............. +................ Pat6_Scan_comp_at_speed_m:31647 1 6163356 32642 ..l............. +................ Pat6_Scan_comp_at_speed_m:31650 1 6163359 32645 ..h............. +................ Pat6_Scan_comp_at_speed_m:31656 1 6163365 32651 .......h........ +................ Pat6_Scan_comp_at_speed_m:31697 1 6163406 32692 ...l.h.......... +................ Pat6_Scan_comp_at_speed_m:31703 1 6163412 32698 .....h.......... +................ Pat6_Scan_comp_at_speed_m:31704 1 6163413 32699 ...l.l.......... +................ Pat6_Scan_comp_at_speed_m:31705 1 6163414 32700 ...h.h.......... +................ Pat6_Scan_comp_at_speed_m:31706 1 6163415 32701 ...h.h.......... +................ Pat6_Scan_comp_at_speed_m:31707 1 6163416 32702 ...l.h.......... +................ Pat6_Scan_comp_at_speed_m:31708 1 6163417 32703 ...l.l.......... +................ Pat6_Scan_comp_at_speed_m:31713 1 6163422 32708 .........l...... +................ Pat6_Scan_comp_at_speed_m:31715 1 6163424 32710 ........l....... +................ Pat6_Scan_comp_at_speed_m:31723 1 6163432 32718 .........l...... +................ Pat6_Scan_comp_at_speed_m:31725 1 6163434 32720 ........lh...... +................ Pat6_Scan_comp_at_speed_m:31728 1 6163437 32723 ........l....... +................ Pat6_Scan_comp_at_speed_m:31730 1 6163439 32725 .........h...... +................ Pat6_Scan_comp_at_speed_m:31738 1 6163447 32733 .ll............. +................ Pat6_Scan_comp_at_speed_m:31739 1 6163448 32734 .hh............. +................ Pat6_Scan_comp_at_speed_m:31740 1 6163449 32735 .lh.hl.......... +................ Pat6_Scan_comp_at_speed_m:31750 1 6163459 32745 ....lh.......... +................ Pat6_Scan_comp_at_speed_m:31751 1 6163460 32746 ....hh.......... +................ Pat6_Scan_comp_at_speed_m:31752 1 6163461 32747 ....hl.......... +................ Pat6_Scan_comp_at_speed_m:31757 1 6163466 32752 .......l.h...... +................ Pat6_Scan_comp_at_speed_m:31779 1 6163488 32774 .h.............. +................ Pat6_Scan_comp_at_speed_m:31781 1 6163490 32776 .h.............. +................ Pat6_Scan_comp_at_speed_m:31808 1 6163517 32803 ..h............. +................ Pat6_Scan_comp_at_speed_m:31818 1 6163527 32813 .h.l............ +................ Pat6_Scan_comp_at_speed_m:31819 1 6163528 32814 .h.h............ +................ Pat6_Scan_comp_at_speed_m:31824 1 6163533 32819 .l.h............ +................ Pat6_Scan_comp_at_speed_m:31825 1 6163534 32820 .......hh....... +................ Pat6_Scan_comp_at_speed_m:31829 1 6163538 32824 .hh............. +................ Pat6_Scan_comp_at_speed_m:31831 1 6163540 32826 .l....l..l...... +................ Pat6_Scan_comp_at_speed_m:31833 1 6163542 32828 .lh............. +................ Pat6_Scan_comp_at_speed_m:31836 1 6163545 32831 .hl............. +................ Pat6_Scan_comp_at_speed_m:31837 1 6163546 32832 .lh............. +................ Pat6_Scan_comp_at_speed_m:31838 1 6163547 32833 .lh............. +................ Pat6_Scan_comp_at_speed_m:31839 1 6163548 32834 .lh............. +................ Pat6_Scan_comp_at_speed_m:31840 1 6163549 32835 .lh............. +................ Pat6_Scan_comp_at_speed_m:31842 1 6163551 32837 .lh............. +................ Pat6_Scan_comp_at_speed_m:31843 1 6163552 32838 .hl............. +................ Pat6_Scan_comp_at_speed_m:31845 1 6163554 32840 .hh............. +................ Pat6_Scan_comp_at_speed_m:31859 1 6163568 32854 ....h........... +................ Pat6_Scan_comp_at_speed_m:31867 1 6163576 32862 ...l............ +................ Pat6_Scan_comp_at_speed_m:31868 1 6163577 32863 ...h............ +................ Pat6_Scan_comp_at_speed_m:31869 1 6163578 32864 ...h............ +................

Now the script should match with the "h" and "l" characters of the pins with the corresponding pins at the top . Each dot "." in the 5th Column of the input file is representing the pin which is there at the top for example first "." represents PD10 , second "." represents PD9 etc ...So wherever the script match with h and l , it should pick the corresponding pin of the top and print in the output file Output file should be having the 4th Column which is cycle number and the pin name and the "h" or "l" character of the corresponding pin For example , Output file should be like this Output file

Cycle Pin-name expected-value 25701 PD3 h 25794 PD4 h 25794 PD9 h

Thanks Kshitij

Replies are listed 'Best First'.
Re: Need help in getting a perl script for below logic
by tybalt89 (Monsignor) on Feb 09, 2019 at 07:54 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1229657 use strict; use warnings; my $rawpins; do { local $/ = ''; $rawpins = <DATA>; $rawpins =~ s/^\s*PINS//; }; $rawpins =~ s/^ *//gm; my $pins = ''; $pins .= "\n" while $rawpins =~ s/^./ $pins .= $&; '' /gem; tr/[]//d for my @pins = split ' ', $pins; 1 until <DATA> =~ /----/; while( <DATA> ) { my ($cycle, $hl) = (split)[3, 4]; print "$cycle $pins[$-[0]] $1\n" while $hl =~ s/.*\K([hl])/./; } __DATA__ PINS PPPPPPPPPPPPPPP DDDDDDDDDDDEEEE [[[[[[[[[[[[[[[[ 198765432105432 0]]]]]]]]]]]]]]] ] VecAddr RptCnt VmemAddr Cycle ------------------------------- ------ -------- ----- ---------------- +---------------- Pat6_Scan_comp_at_speed_m:24791 1 6156500 25701 .......h........ +................ Pat6_Scan_comp_at_speed_m:24884 1 6156593 25794 .h....h......... +................ Pat6_Scan_comp_at_speed_m:24891 1 6156600 25801 ...h.....h...... +................ Pat6_Scan_comp_at_speed_m:26759 1 6158468 27689 ................ +................ Pat6_Scan_comp_at_speed_m:29196 1 6160905 30160 ................ +................ Pat6_Scan_comp_at_speed_m:30671 1 6162380 31650 ....h.h......... +................ Pat6_Scan_comp_at_speed_m:30982 1 6162691 31966 .....hl......... +................ Pat6_Scan_comp_at_speed_m:30985 1 6162694 31969 .....h.......... +................ Pat6_Scan_comp_at_speed_m:30986 1 6162695 31970 .....l.......... +................ Pat6_Scan_comp_at_speed_m:31276 1 6162985 32266 ......h......... +................ Pat6_Scan_comp_at_speed_m:31562 1 6163271 32557 ................ +................ Pat6_Scan_comp_at_speed_m:31563 1 6163272 32558 ................ +................ Pat6_Scan_comp_at_speed_m:31593 1 6163302 32588 .......h........ +................ Pat6_Scan_comp_at_speed_m:31594 1 6163303 32589 .......h........ +................ Pat6_Scan_comp_at_speed_m:31637 1 6163346 32632 ........l....... +................ Pat6_Scan_comp_at_speed_m:31639 1 6163348 32634 ........l....... +................ Pat6_Scan_comp_at_speed_m:31645 1 6163354 32640 ..h............. +................ Pat6_Scan_comp_at_speed_m:31646 1 6163355 32641 ..l............. +................ Pat6_Scan_comp_at_speed_m:31647 1 6163356 32642 ..l............. +................ Pat6_Scan_comp_at_speed_m:31650 1 6163359 32645 ..h............. +................ Pat6_Scan_comp_at_speed_m:31656 1 6163365 32651 .......h........ +................ Pat6_Scan_comp_at_speed_m:31697 1 6163406 32692 ...l.h.......... +................ Pat6_Scan_comp_at_speed_m:31703 1 6163412 32698 .....h.......... +................ Pat6_Scan_comp_at_speed_m:31704 1 6163413 32699 ...l.l.......... +................ Pat6_Scan_comp_at_speed_m:31705 1 6163414 32700 ...h.h.......... +................ Pat6_Scan_comp_at_speed_m:31706 1 6163415 32701 ...h.h.......... +................ Pat6_Scan_comp_at_speed_m:31707 1 6163416 32702 ...l.h.......... +................ Pat6_Scan_comp_at_speed_m:31708 1 6163417 32703 ...l.l.......... +................ Pat6_Scan_comp_at_speed_m:31713 1 6163422 32708 .........l...... +................ Pat6_Scan_comp_at_speed_m:31715 1 6163424 32710 ........l....... +................ Pat6_Scan_comp_at_speed_m:31723 1 6163432 32718 .........l...... +................ Pat6_Scan_comp_at_speed_m:31725 1 6163434 32720 ........lh...... +................ Pat6_Scan_comp_at_speed_m:31728 1 6163437 32723 ........l....... +................ Pat6_Scan_comp_at_speed_m:31730 1 6163439 32725 .........h...... +................ Pat6_Scan_comp_at_speed_m:31738 1 6163447 32733 .ll............. +................ Pat6_Scan_comp_at_speed_m:31739 1 6163448 32734 .hh............. +................ Pat6_Scan_comp_at_speed_m:31740 1 6163449 32735 .lh.hl.......... +................ Pat6_Scan_comp_at_speed_m:31750 1 6163459 32745 ....lh.......... +................ Pat6_Scan_comp_at_speed_m:31751 1 6163460 32746 ....hh.......... +................ Pat6_Scan_comp_at_speed_m:31752 1 6163461 32747 ....hl.......... +................ Pat6_Scan_comp_at_speed_m:31757 1 6163466 32752 .......l.h...... +................ Pat6_Scan_comp_at_speed_m:31779 1 6163488 32774 .h.............. +................ Pat6_Scan_comp_at_speed_m:31781 1 6163490 32776 .h.............. +................ Pat6_Scan_comp_at_speed_m:31808 1 6163517 32803 ..h............. +................ Pat6_Scan_comp_at_speed_m:31818 1 6163527 32813 .h.l............ +................ Pat6_Scan_comp_at_speed_m:31819 1 6163528 32814 .h.h............ +................ Pat6_Scan_comp_at_speed_m:31824 1 6163533 32819 .l.h............ +................ Pat6_Scan_comp_at_speed_m:31825 1 6163534 32820 .......hh....... +................ Pat6_Scan_comp_at_speed_m:31829 1 6163538 32824 .hh............. +................ Pat6_Scan_comp_at_speed_m:31831 1 6163540 32826 .l....l..l...... +................ Pat6_Scan_comp_at_speed_m:31833 1 6163542 32828 .lh............. +................ Pat6_Scan_comp_at_speed_m:31836 1 6163545 32831 .hl............. +................ Pat6_Scan_comp_at_speed_m:31837 1 6163546 32832 .lh............. +................ Pat6_Scan_comp_at_speed_m:31838 1 6163547 32833 .lh............. +................ Pat6_Scan_comp_at_speed_m:31839 1 6163548 32834 .lh............. +................ Pat6_Scan_comp_at_speed_m:31840 1 6163549 32835 .lh............. +................ Pat6_Scan_comp_at_speed_m:31842 1 6163551 32837 .lh............. +................ Pat6_Scan_comp_at_speed_m:31843 1 6163552 32838 .hl............. +................ Pat6_Scan_comp_at_speed_m:31845 1 6163554 32840 .hh............. +................ Pat6_Scan_comp_at_speed_m:31859 1 6163568 32854 ....h........... +................ Pat6_Scan_comp_at_speed_m:31867 1 6163576 32862 ...l............ +................ Pat6_Scan_comp_at_speed_m:31868 1 6163577 32863 ...h............ +................ Pat6_Scan_comp_at_speed_m:31869 1 6163578 32864 ...h............ +................
      Thank you very much for your kind help !
Re: Need help in getting a perl script for below logic
by poj (Abbot) on Feb 09, 2019 at 07:50 UTC

    Try

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $infile = 'pins.txt'; open my $fh,'<',$infile or die "Could not open $infile : $!"; my @pin; my $flag = 0; my $count = 0; while (<$fh>){ chomp; ++$count; next unless /\S/; # skip blanklines next if /------------/; # end of pin layout if (/VecAddr/){ print Dumper \@pin; s/[^A-Z0-9]//g for @pin; # remove brackets $flag = 1; print join "\t",qw(Cycle Pin-name expected-value),"\n"; next; } if ($flag == 0){ # build pin layout my $str = substr($_,54,16); for my $i (0..15){ $pin[$i] .= substr($str,$i,1) } } else { # process data my @f = split /\s+/,$_; my @p = split '',$f[4]; my $i = 0; for (@p){ if (/[hl]/){ printf "%s\t%s\t%s\n",$f[3],$pin[$i],$_; } ++$i; } } } close $fh; print "$count records scanned from $infile\n";
    poj
Re: Need help in getting a perl script for below logic
by kcott (Archbishop) on Feb 09, 2019 at 22:40 UTC

    G'day Kshitij,

    This seemed like an interesting problem to get the mental juices flowing on a Sunday morning, so here's my take on it.

    I split the problem into two discrete areas, processing the metadata (before the "---" line) and the pin data (after the "---" line) separately:

    if ($meta) { ... } else { ... }

    I didn't know if the ']', on a line by itself, was part of the PINS data or a sentinel indicating the end of it. I used 'VecAddr' as the sentinel: you may want to change that. Also, the PINS lines containing a '[' or ']' were different lengths to the other lines: I don't know if that's significant.

    Anyway, here's the code:

    #!/usr/bin/env perl use strict; use warnings; my $meta = 1; my $pin_meta = 1; my @pins; while (<DATA>) { chomp; next unless /\S/; if ($meta) { if (0 == index $_, '-') { $meta = 0; next; } else { if ($pin_meta) { if (/^\s*VecAddr/) { $pin_meta = 0; } else { my @chars = map { /[[\]]/ ? '' : $_ } split //, (/(\S+)\s*$/)[0]; $pins[$_] .= $chars[$_] for 0 .. $#chars; } } } } else { my ($cycle, $pin_str) = (split)[3,4]; my @pin_str_chars = split //, $pin_str; for (0 .. $#pin_str_chars) { next unless $pin_str_chars[$_] =~ /[hl]/; print "$cycle $pins[$_] $pin_str_chars[$_]\n"; } } } __DATA__ PINS PPPPPPPPPPPPPPP DDDDDDDDDDDEEEE [[[[[[[[[[[[[[[[ 198765432105432 0]]]]]]]]]]]]]]] ] + + + + VecAddr RptCnt VmemAddr Cycle + ------------------------------- ------ -------- ----- ---------------- +---------------- Pat6_Scan_comp_at_speed_m:24791 1 6156500 25701 .......h........ +................ Pat6_Scan_comp_at_speed_m:24884 1 6156593 25794 .h....h......... +................ Pat6_Scan_comp_at_speed_m:24891 1 6156600 25801 ...h.....h...... +................ Pat6_Scan_comp_at_speed_m:26759 1 6158468 27689 ................ +................ Pat6_Scan_comp_at_speed_m:29196 1 6160905 30160 ................ +................ Pat6_Scan_comp_at_speed_m:30671 1 6162380 31650 ....h.h......... +................ Pat6_Scan_comp_at_speed_m:30982 1 6162691 31966 .....hl......... +................ Pat6_Scan_comp_at_speed_m:30985 1 6162694 31969 .....h.......... +................ Pat6_Scan_comp_at_speed_m:30986 1 6162695 31970 .....l.......... +................

    As you can see, I've only used enough of your PINS lines for a representative sample (unless I've missed something important in the omitted lines). For future reference, unless a lot of input data is necessary to describe the problem in full, please consider trimming it to a representative subset. Here's the output from that script:

    25701 PD3 h 25794 PD9 h 25794 PD4 h 25801 PD7 h 25801 PD1 h 31650 PD6 h 31650 PD4 h 31966 PD5 h 31966 PD4 l 31969 PD5 h 31970 PD5 l

    Your PINS data is oddly ordered: leading alphabetics are ascending and trailing numerics are descending. My 2nd and 3rd lines are in a different order to your minimal output description: you may need to add some sorting.

    — Ken