in reply to Re^2: Formatted output file with data processing
in thread Formatted output file with data processing

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Formatted output file with data processing
by kshitij (Sexton) on Oct 03, 2019 at 04:18 UTC

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

    Thanks Kshitij

      The problematic statement is
          my $fmt = $odd_format{$register} // $default_format;
      which uses the  // (defined-or) operator introduced with Perl version 5.10.

      Because any value of  $odd_format{$register} (i.e., a format string) is highly unlikely to be false (see What is true and false in Perl?), it's almost certainly ok to use the  || (logical-or) operator instead (all the following alternatives are untested):
          my $fmt = $odd_format{$register} || $default_format;

      An absolutely bullet-proof alternative uses exists and the  ? : (ternary) operator:
          my $fmt = exists($odd_format{$register}) ? $odd_format{$register} : $default_format;


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

      Your installation of perl is over 12 years old. Either upgrade to a vaguely recent version or at the very worst stipulate which version you have (and which O/S: make, model, version) and state why you are unable to upgrade.

        I am not having administrator rights so difficult to upgrade.