Novice_Monk23 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am new to perl, I really didn't have time to go through perl nicely and was assigned this task for completion. I have written this code with all my broken knowledge and I am aware it must be horrible.
Please have a look if you can help. If that's too much, pointing out errors will work out fine for me, I'll rectify it myself.
I will really love some help rather than criticism
I have an input csv file with 18 column referred as Column A to Column R. I need a formatted output file and error file on based on these conditions:
1. Replace + and = in column D by blank
2. Move all rows to error files which has BC entries in column O
3. Delete rows for 0000,1111,2222 in column A
4. If column A has 2323 column O should only have AA, if not move to error
5. If column A has 1212, change it to 'a'.. If 2121 then change to aa and if 3131 change to ba
6. If column A has 6767,9898 or 5656 and column D contains '09 09' type 'AA' in column G.
The written code has lot of syntax and logical errors.
Many of them are because of wrong declaration, use of strict etc.
But if not, there are logical errors which leads to printing only one row in output file and no result in error file.
I am also facing difficulties in parsing csv line by line and seperating fields in commas.
Please have a look if you can help.
Many many thanks!
# !/user/bin/perl use strict; use file :: basename use file :: copy #command line arguments my #input1=$ARGV[0] my #string; # array to store extracted dields my @records; my @fieldvalues; # assign paths for output files my $destinationapth="D/perl/output/"; my $destination="$destinationpath."OutputFile.txt"; my $destination1="$destinationpath."Error.txt"; open(IN, ">$destination"); opne(IN_Err,">$destination1"); open(DATA1,"<$input1"); my $a=0; while(<DATA1>) { $String="."; $String=$_; @records=Split(/\n/,$String); foreach $fieldvalues(@records) { @fieldvalues=split(/,/,$String); #to split columns from each r +ow in csv file foreach my $element(@fieldvalues) { # to remove linebreaks & whitespaces $element= ~s/\r|\n//g; } # Deleting values not needed in output if ($fieldvalues[0] eq '1111' || $fieldvalues[0] eq '2222' || +$fieldvalues[0] eq '0000') { #do nothing } else { #checking COL A for 2323 & column O except than AA if($fieldvalues[0] eq '2323' && $fieldvalues[14] ne 'AA') { #move to err file PRINT IN_Err ("@records\n"); } #replace "+/=" with " "(space) in col D $ fieldvalues[3] = ~s/[+=]/ /g; #replacing Col A values if($fieldvalues[0] eq '1212') { $fieldvalues[0] ='a'; } if($fieldvalues[0] eq '2121') { $fieldvalues[0] ='aa'; } if($fieldvalues[0] eq '3131') { $fieldvalues[0] ='ba'; } } # if for specific COL A values, column D contains specific tex +t, replacing column G value if ($fieldvalues[0] eq '9898' || $fieldvalues[0] eq '6767' || +$fieldvalues[0] eq '5656') { if(index($fieldvalues[3], '09 09')!==-1) { $fieldvalues[6]='AA; } #printing in formatted file print IN ("@records\n"); } } } Close (DATA);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl: Syntax Errors, Help Needed
by Tux (Canon) on Jun 16, 2016 at 12:40 UTC | |
|
Re: Perl: Syntax Errors, Help Needed
by Discipulus (Canon) on Jun 16, 2016 at 11:51 UTC | |
|
Re: Perl: Syntax Errors, Help Needed
by choroba (Cardinal) on Jun 16, 2016 at 12:12 UTC | |
by Laurent_R (Canon) on Jun 16, 2016 at 17:14 UTC | |
by Anonymous Monk on Jun 16, 2016 at 13:45 UTC | |
by Preceptor (Deacon) on Jun 20, 2016 at 15:58 UTC | |
|
Re: Perl: Syntax Errors, Help Needed
by GotToBTru (Prior) on Jun 16, 2016 at 12:13 UTC |