in reply to Formatted output file with data processing

Read the input line by line. If the line contains patX, just verify that you have stored X-1 values so far. Otherwise, store the value into an array; once the array has 5 elements, print the register name and the elements.
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @values; while (<>) { if (my ($size) = /^pat([0-9])/) { $size == 1 + @values or die "Unexpected $_"; next } my ($register, $value) = /(\S+).*\}([0-9])/; say join "\t", $register, splice @values if 5 == push @values, $value; }
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Formatted output file with data processing
by Don Coyote (Hermit) on Sep 24, 2019 at 13:32 UTC

    choroba, I like how you get at the values without even considering a hash here. Also the uppercase S to capture the first number in the matching pattern.

Re^2: Formatted output file with data processing
by kshitij (Sexton) on Sep 24, 2019 at 17:34 UTC

    Thanks a lot !

    Could you help me out by creating in the format as stated in output file

    Reg pat1 pat2 pat3 pat4 pat5 U_TOP_LOGIC/i_reg_2_/Q 1 1 0 1 1 U_TOP_LOGIC/i_reg_3_/Q 1 1 1 1 1 U_TOP_LOGIC/i_reg_4_/Q 1 0 1 0 1 ########################################################

    Thanks

    Kshitij

      This is an extension of choroba's code here with more tolerance for the blank and comment lines apparently (?) present in the OPed example input file (k.dat below), and reproducing exactly the output format shown here. Add oddball output formats for individual registers as needed.

      Script format_multiline_for_output_1.pl:

      use 5.010; # needs // operator use warnings; use strict; my $default_format = "%-25s%d %d %d %d %d\n"; my %odd_format = ( 'U_TOP_LOGIC/i_reg_2_/Q' => "%-25s%d %d %d %d %d\n", ); print "Reg pat1 pat2 pat3 pat4 pat5\n\n"; my @values; while (<>) { if (/^\s*$/ or /^\s*#/) { # blank or comment line # do nothing } elsif (my ($size) = /^pat([0-9])/) { $size == 1 + @values or die "Unexpected '$_'"; } else { my ($register, $value) = /(\S+).*\}([0-9])/; my $fmt = $odd_format{$register} // $default_format; printf $fmt, $register, splice @values if 5 == push @values, $value; } } print "########################################################\n";
      Output:
      c:\@Work\Perl\monks\kshitij>perl format_multiline_for_output_1.pl < k. +dat Reg pat1 pat2 pat3 pat4 pat5 U_TOP_LOGIC/i_reg_2_/Q 1 1 0 1 1 U_TOP_LOGIC/i_reg_3_/Q 1 1 1 1 1 U_TOP_LOGIC/i_reg_4_/Q 1 0 1 0 1 ########################################################
      (k.dat taken from the OPed example input file.)


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

        Thanks a lot for your respond but somehow this is not available "use 5.010"

        Thanks Kshitij