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

    Homework or not, this is too nice a problem to resist to show the beauty of Text::CSV_XS. Sorry.

    use 5.18.2; use warnings; use Data::Peek; use Text::CSV_XS qw( csv ); my @f = ("A".."R"); csv (in => "file.csv", headers => \@f, filter => { A => sub { if ($_ eq "2323" && $_{O} ne "AA") { $_[0]->say (*STDERR, [ "ERROR", @_{@f} ]); return 0; } $_ ne "0000" && $_ ne "1111" && $_ ne "2222"; }, O => sub { if ($_ eq "BC") { $_[0]->say (*STDERR, [ "ERROR", @_{@f} ]); return 0; } 1; } }, on_in => sub { $_{D} =~ tr/+=/ /; $_{A} =~ s/^1212$/a/; $_{A} =~ s/^2121$/aa/; $_{A} =~ s/^3131$/ba/; $_{A} =~ m/^(?:6767|9898|5656)$/ && $_{D} =~ m/90 09/ and $_{ +G} = "AA"; }, );

    I tested that with

    A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h A,B,C,foo + bar = F,E,F,G,H,I,J,K,L,M,N,O,P,Q,R A,B,C,D,E,F,G,H,I,J,K,L,M,N,BC,P,Q,R 0000,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 1111,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 2222,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 2323,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 2323,B,C,D,E,F,G,H,I,J,K,L,M,N,AA,P,Q,R 1212,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 2121,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 3131,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 6767,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 6767,B,C,D09 09D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R

    =>

    ERROR,A,B,C,D,E,F,G,H,I,J,K,L,M,N,BC,P,Q,R ERROR,2323,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h A,B,C,"foo bar F",E,F,G,H,I,J,K,L,M,N,O,P,Q,R 2323,B,C,D,E,F,G,H,I,J,K,L,M,N,AA,P,Q,R a,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R aa,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R ba,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 6767,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R 6767,B,C,"D09 09D",E,F,G,H,I,J,K,L,M,N,O,P,Q,R

    Enjoy, Have FUN! H.Merijn
Re: Perl: Syntax Errors, Help Needed
by Discipulus (Canon) on Jun 16, 2016 at 11:51 UTC
    you can start changing from use file :: basename to use File::Basename

    Then you can add

    use warnings; use diagnostics; # produce verbose warning diagnostics

    and fix warnings you get.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Perl: Syntax Errors, Help Needed
by choroba (Cardinal) on Jun 16, 2016 at 12:12 UTC
    Crossposted to StackOverflow. Crossposting is OK, but it's considered polite to inform about it, in order to spare people not attending both sites hacking a solution to a problem already solved at the other end of the internet.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Yes, If people would not be acting as if I am trying to kill them on stackoverflow?

        Well, it is kinda impolite to post code that doesn't actually work in the first place, due to basic syntax errors, and expect people to sort that out for you. Of course, that's true here too.

Re: Perl: Syntax Errors, Help Needed
by GotToBTru (Prior) on Jun 16, 2016 at 12:13 UTC

    Looks like homework.

    Fix the syntax errors first. Then run the program in the debugger to check the logic and program flow.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)