CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help Needed :(

 



The_novice
New User

Jun 16, 2016, 4:11 AM

Post #1 of 3 (1347 views)
Help Needed :( Can't Post

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 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!


Code
# !/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 row 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 text, 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");
}
}



(This post was edited by FishMonger on Jun 16, 2016, 7:44 AM)


FishMonger
Veteran / Moderator

Jun 16, 2016, 8:04 AM

Post #2 of 3 (1335 views)
Re: [The_novice] Help Needed :( [In reply to] Can't Post

Cross posted on perlmonks and stackoverflow.
http://perlmonks.org/?node_id=1165839
http://stackoverflow.com/questions/37856440/perl-syntax-logical-errors

The answers you will receive here will be along the same lines as what you've already received. So, start by fixing the problems they pointed out and if you still need help after that, then post your updated script and a more refined/specific question on the part(s) that are giving you trouble. You should also include the errors and warnings that your script produces.


(This post was edited by FishMonger on Jun 16, 2016, 8:05 AM)


BillKSmith
Veteran

Jun 16, 2016, 8:30 AM

Post #3 of 3 (1332 views)
Re: [The_novice] Help Needed :( [In reply to] Can't Post

Your code is actually better than you seem to think. Before we can fix any logic errors, we must start with valid perl. It is important to keep "use strict;" and add "use warnings;". Removing them does not remove errors, it only hides them.

When you try to run your code, you get a rather large number of messages. Most of these are due to minor mistakes (Spelling and capitalization of variable names, missing semicolons or dollar signs, or extra quote marks.) Look for a few things that are easy to fix and fix them. When to try to run the code again, you probably will be surprised by how much smaller the list of messages is. Repeat this process until there are no more errors, or you a truly stuck. (ask for help with the specific errors that are a problem.) Then post your valid code. Explain what it does wrong when you try to process your sample data.

In the mean time, please post a small sample of your data. Include at least one record to test each requirement. All fields not mentioned in the requirements may contain meaningless text.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives