Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

please help me to resolve the Line comments and appending issue

by suno (Acolyte)
on Aug 08, 2012 at 12:36 UTC ( #986253=perlquestion: print w/ replies, xml ) Need Help??
suno has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to run the program below:
#Perl program # Variable to get the command line arguments # Command line arguments are Source and Destination filename $src_filename = $ARGV[0]; $var_filename = $ARGV[1]; $tgt_filename = $ARGV[2]; $mac_filename = $ARGV[2]; # Statement to read data in Source file using file handler # Declaring the file handlers to read the input file open(IFILE,"<$src_filename") || die "error in opening source file\n"; open(OFILE2,"+>$tgt_filename") || die "error in opening dest file\n"; open(OFILE1,"+>$var_filename") || die "error in opening dest file\n"; open(OFILE,"<$mac_filename") || die "error in opening macro file\n"; # Read input file into a input array to process the data @Read_Array = (); # Array initialization @Read_Array = <IFILE>; # Read all contents of file into a +n array @Var_File_Array = (); # Output array for modified assmeb +ler code @Tgt_File_Array = (); # Temporary variables $Line = ''; $Indx = 0; # Variable for array indexes # Read Array print @Read_Array; # Preprocessing for each line in an array foreach $Line (@Read_Array) { #Read only 1 - 72 columns data $Line =~ /.{72}/; $Line = $&; $Line = $Line."\n"; #$Line =~ /(.+)*\s+(.+)$/; $Temp = $Line; #Remove evrything after column 40 fromt he code $Line =~ /.{40}/; $description = $'; $Line = $&; chomp($description); $description =~s/\s+$//g; $Line =~s/\(/\'/g; $Line =~s/\)/\'/g; if($Line =~/([^\s]+)\s+(DC|DS|EQU)\s+(A|X|C|H|F|D|0X|0D|0C)+(L)* +([^\'\(\s]+)*(\'(.+)\')*\s*/i) { $Print_Line = " "; $Print_Line = $1.",".$3.",".$5.",".$7.",".$description."\n"; push(@Var_File_Array,$Print_Line); } else { if($Temp!~/^\s*\*/ and $Temp!~/^\s+$/) #lines other than comment +s and line spaces { push(@Tgt_File_Array,$Temp); if($Temp=~/\s+(EQU)\s+(\*)/i) #remove EQU * from the state +ments { $Temp = $`; $Temp = $Temp."\n"; pop @Tgt_File_Array; push(@Tgt_File_Array,$Temp); if(length($Temp)>1) { @split_words = split(' ',$Temp); #to remove line co +mments #pop @Tgt_File_Array; push(@Tgt_File_Array,@split_words[0],' ',@split_words[1]); } } } } }

here when i give my input as :

VOR#000 EQU * MVC A,B move B to A CLC VAR1,VAR2 compare VAR2 != VAR1 BNE COMP EJECT
my expected output should be :
VOR#000 MVC A,B CLC VAR1,VAR2 BNE COMP EJECT
but..here i am facing some issues with removing the line comments and i need to append "BNE COMP" along with the previous line...i am very new to perl...can you please help me out to resolve this issue?

Comment on please help me to resolve the Line comments and appending issue
Select or Download Code
Re: please help me to resolve the Line comments and appending issue
by aitap (Deacon) on Aug 08, 2012 at 14:32 UTC
    Here are some comments to your program:
    1. Always
      use warnings; use strict;
      It can look inconvinient, but it really helps to prevent some stupid bugs.
    2. This:
      $src_filename = $ARGV[0]; $var_filename = $ARGV[1]; $tgt_filename = $ARGV[2]; $mac_filename = $ARGV[2];
      can be rewritten as:
      die "Usage: four arguments\n" unless $#ARGV == 3; my ($src_filename,$var_filename,$tgt_filename,$mac_filename) = @ARGV;
    3. You can process the file line-by-line, without any need to read the whole file into memory:
      while (defined my $line = <IFILE>) { chomp; # remove \n from the end of the line ... }
    4. #Read only 1 - 72 columns data $Line =~ /.{72}/; $Line = $&; $Line = $Line."\n";
      Are you sure you are working with fixed-length strings? Anyway, $line = substr($line,0,72); is easier and faster.
    5. $Temp = $Line; #Remove evrything after column 40 fromt he code $Line =~ /.{40}/; $description = $'; $Line = $&; chomp($description); $description =~s/\s+$//g;
      This also can be done easier: ($line,$description) = ($line=~/^(.{40})(.*)\s+$/);
      I suspect, there is a more right way to do it (perhaps, using split), but I can't figure it out now.
    6. $Line =~s/\(/\'/g; $Line =~s/\)/\'/g; if($Line =~/([^\s]+)\s+(DC|DS|EQU)\s+(A|X|C|H|F|D|0X|0D|0C)+(L)* +([^\'\(\s]+)*(\'(.+)\')*\s*/i) { $Print_Line = " "; $Print_Line = $1.",".$3.",".$5.",".$7.",".$description."\n"; push(@Var_File_Array,$Print_Line); } else { if($Temp!~/^\s*\*/ and $Temp!~/^\s+$/) #lines other than comment +s and line spaces { push(@Tgt_File_Array,$Temp); if($Temp=~/\s+(EQU)\s+(\*)/i) #remove EQU * from the state +ments { $Temp = $`; $Temp = $Temp."\n"; pop @Tgt_File_Array; push(@Tgt_File_Array,$Temp); if(length($Temp)>1) { @split_words = split(' ',$Temp); #to remove line co +mments #pop @Tgt_File_Array; push(@Tgt_File_Array,@split_words[0],' ',@split_words[1]); } } }
      Oops. Perhaps this logic can be optimised:
      if($line =~/([^\s]+)\s+(DC|DS|EQU)\s+(A|X|C|H|F|D|0X|0D|0C)+(L)*([^\'\ +(\s]+)*(\'(.+)\')*\s*/i) { ($line,my $description) = ($line=~/^(.{40})(.*)\s+$/); # do the actua +l $line modification only there $line =~ tr/\(\)/''/g; push(@Var_File_Array,$1.",".$3.",".$5.",".$7.",".$description."\n"); } elsif (!($line=~/^\s*\*|^\s*$/)) { push(@Tgt_File_Array,$line); if ($line =~ s/\s+EQU\s+\*.*$/i) { pop @Tgt_File_Array; push(@Tgt_File_Array,$line."\n"); length $line > 0 && push @Tgt_File_Array,(join " ",(split ' ',$line) +[0,1]); } }
      Note: this piece of code was untested. To my mind, it needs to be completely rewritten.
    Your script seems rather complicated. Are you doing such complex work? Perhaps everything can be done a lot easier if you tell us your original task with more small source and expected data samples.
    EDIT: minor code changes
    Sorry if my advice was wrong.
      Hi, perhaps i am working on a complex task... my actual source is : The following is my actual input.
      *** *************************************************************** ** +* *** THE FOLLOWING IS THE SAMPLE CODE ** +* *** -------------------- ** +* *** ** +* *** ** +* *** ALL COMMENTS,LINE COMMENTS AND LINE SPACES HAS TO BE REMOVED ** +* *** ** +* *** ** +* *** ** +* *** ** +* *** *************************************************************** ** +* + + BEGIN EQU * + L R1,0(R1) LOAD REGISTER R1 TO R1 + CLI R3,0(,R1) BNE R4,2(,R1) LTR R3,R3 REGISTER R3 > 0? + @PUT FFILE,ADR=ZEILE MACRO + TKT100 DC x'0300' TG-ID + TKT300 DC X'000000' SYSTEMZEIT + JSKDAT DS XL4 DATUM FUER JAHRESSCHLUSSKURS + + KURSDA DS XL4 KURS-DATUM 30.06.XXXX

      my requirements are: generate 3 files 1)variable file , 2)code file ,3)macro file things has to be done: 1) in VARIABLE FILE: in statement

      "KURSDA   DS    XL4'1000'   KURS-DATUM 30.06.XXXX " i have to move the following values to the variable file...
      vairable length datatype value description + KURSDA 4 X 1000 KURS-DATUM 30.06.XX +XX
      2)in CODE FILE: here i have to modify the code: 1)remove line comments 2)remove "EQU *" from the code 3)append statement starting with BNE to the previous statement starting with CLI 4)all statements with DC,DS and EQU should be removed the final output should look like:
      BEGIN + L R1,0(R1) CLI R3,0(,R1) BNE R4,2(,R1) LTR R3,R3 @PUT FFILE,ADR=ZEILE

      3)in MACRO FILE: and L,CLI,BNE,LTR,DC,DS all are keywords... i need to capture all other than keywords and push to the variable file. foe example: "@PUT  FFILE,ADR=ZEILE            MACRO   " here @PUT is not a keyword. so that should be pushed to the variable file and the next argument under the column value and the 3rd argument"MACRO" under description..that is,it should look like,

      vairable value description @PUT FFILE,ADR=ZEILE MACRO
        little correctionin the 3rd point: 3)in MACRO FILE: and L,CLI,BNE,LTR,DC,DS all are keywords... i need to capture all other than keywords and push to the macro file. foe example: "@PUT FFILE,ADR=ZEILE MACRO " here @PUT is not a keyword. so that should be pushed to the macro file and the next argument under the column value and the 3rd argument"MACRO" under description..that is,it should look like,
        vairable value description @PUT FFILE,ADR=ZEILE MACRO
        Here is the code based on your example:
        #!/usr/bin/perl use warnings; use strict; use Getopt::Std; my %options; getopt("ivcm",\%options); do { die "Usage: $0 -i <input file> -v <variable file> -c <code file> -m < +macro file>\n" unless defined $options{$_} } for qw/i v c m/; open my $input, "<", $options{i} || die "$options{i}: $!\n"; open my $code, ">", $options{c} || die "$options{c}: $!\n"; open my $var, ">", $options{v} || die "$options{v}: $!\n"; open my $macro, ">", $options{m} || die "$options{m}: $!\n"; while (defined(my $line = <$input>)) { chomp $line; next if $line=~/^\s*$/; next if $line=~/^\s*\*/; $line=~s/EQU\s+\*//; $line=~s/\s*$//; my @fields = split /\s+/,$line,4; shift @fields until $fields[0] ne ''; if ($fields[0] eq 'BEGIN') { print $code join ("\t",@fields),"\n"; } elsif ($fields[0] =~ /^(BEGIN|L|CLI|BNE|LTR)$/) { #code pop @fields; print $code (join "\t",@fields),($fields[0] eq 'CLI' ? "" : "\ +n"); } elsif ($fields[0] =~ /^@/) { # macro print $macro (join "\t",@fields),"\n"; pop @fields; print $code (join "\t",@fields),"\n"; } elsif ($fields[1] =~ /^D[CS]$/) { #variable? my ($len) = ($fields[2] =~ /L(\d+)/); #??? my ($type) = $fields[2] =~ /^(.)/; # ??? my ($value) = ($fields[2] =~ /'(\d+)'$/); #??? print $var (join "\t",$fields[0],$len,$type,$value,$fields[3]) +,"\n"; } else { warn "Don't know what to do with line\n$line\n"; } } close $_ || die $! for ($input,$code,$var,$macro);
        It sure contains some mistakes because I had to guess some things about CLI, BNE and variables. Feel free to fix it. If you would state more clearly how to distinguish between code and variables, it will be easier to write the proper code.
        See split, join, shift, pop for more info on this script.
        Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://986253]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-11-22 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (121 votes), past polls