• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

Explain What this script does

Can you please explain what each line of code does to a CSV files.



print "Begin the program: \n";
open(IN,"$file") or die "can not open the inputfile\n";

my @lines = <IN>;

close IN;

for (@lines)
{
/^A(\d+)/ || next;
my $f="c:/Program Files/TIE Commerce/Crunchyroll/in_csv/$1.CRUsinglepo";
open OUT,">>$f" or die $!;
print OUT;
close OUT;
}
0
Navarre_EDI
Asked:
Navarre_EDI
  • 10
  • 8
1 Solution
 
Navarre_EDIAuthor Commented:
These two line are important

for (@lines)
{
/^A(\d+)/ || next;
0
 
sjklein42Commented:
# loop through all the lines in the file, one by one

for (@lines)
{

# for each line that starts with an upper-case "A" followed by a number with one or more digits 0-9...

/^A(\d+)/ || next;

# ... build a string of a file name, substituting the number from the previous step as "$1"

my $f="c:/Program Files/TIE Commerce/Crunchyroll/in_csv/$1.CRUsinglepo";

# open the file whose name we just constructed, echo the contents of the file, and close it

open OUT,">>$f" or die $!;
print OUT;
close OUT;
}
0
 
Navarre_EDIAuthor Commented:
So with the example below and you explain it to me I would have 4 files and 1 with multplie line in there correct?

A661025004,,2011-02-16 06:50:13,,,Michael McFarland,22 Foxtail Lane,,,,Trabuco Canyon,CA,92679,US,(949) 633-7436,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,16.99,
A756435108,,2011-02-16 06:18:35,,,Michael Camacho,6501 Overheart Ln,,,,Columbia,MD,21045,US,(301) 524-3757,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,16.99,
A000063477,,2011-02-16 06:17:00,,,Robert A. Guffin,Greens Farms Academy,35 Beachside Avenue,,,Greens Farms,CT,06838-0998,US,(203) 664-0785,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,18.99,
A961008927,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,Honolulu,HI,96822,US,(808) 344-3096,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,18.99,
A961008927,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,Honolulu,HI,96822,US,(808) 344-3096,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,18.99,
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sjklein42Commented:
First, be sure there are no files already in the output folder when the program starts running.  New data gets appended to the end of the files, so if there are files left over from a previous run they will only get biggger - not reset.

That aside, yes, the data you show should create four output files, one with two lines.
0
 
Navarre_EDIAuthor Commented:
So what happen if I dont have a key number to build the files. Is there another way to build the files?

Example Below and they have CR or LF but the last two will need to be in the same file.

A661025004,,2011-02-16 06:50:13,,,Michael McFarland,22 Foxtail Lane,,,,
B108,,2011-02-16 06:18:35,,,Michael Camacho,6501 Overheart Ln,,,,
123,,2011-02-16 06:17:00,,,Robert A. Guffin,Greens Farms Academy,35 Beachside Avenue,,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,
0
 
Navarre_EDIAuthor Commented:
On your presviuos respond that would only happen appending if the PO number was the same
0
 
sjklein42Commented:
With the code you posted in your question, only keys that start with "A" are recognized.  That is easy to change if it is not the way you want it to work.  Do you want to create a file for every different key?  Why do you need to strip off the "A" at the start of the key?  


In you question above, yes, only the last two lines would be in the same file.  Only keys that match will go into the same file.

If it helps, this version will create a file for every key, not just the ones starting with "A".

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;
my $f="c:/Program Files/TIE Commerce/Crunchyroll/in_csv/$1.CRUsinglepo";
open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
} 

Open in new window

0
 
Navarre_EDIAuthor Commented:
Hello

I am looking and try to run but getting an error

C:\Perl\script>Audyssey_splitter.pl
Begin the program:
Invalid argument at C:\Perl\script\Audyssey_splitter.pl line 22.


 
#!/usr/bin/perl5


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n";
open(IN,"$file") or die "can not open the inputfile\n";

my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;
my $f="c:/Program Files/TIE Commerce/AUDYSSEY/in_csv/$1.AUDsinglepo";
open OUT,">>$f" or die $!;
print OUT;
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}"
0
 
sjklein42Commented:
In Windows, use back-slashes for file paths,  They need to be doubled (quoted) when within a string with double-quotes - like this:

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";

Open in new window

0
 
Navarre_EDIAuthor Commented:
I am still getting the same error at after I made the change.

open OUT,">>$f" or die $!;


Also how about if the data file looks like this and I dont want the first line which is just headers.

PONUMBER,ORDERREFID,ORDERDATE,REQUESTEDDELIVERYDATE,ORDERCANCELDATE,SHIPPING_NAME,SHIPPING_STREET1,SHIPPING_STREET2,
A661025004,,2011-02-16 06:50:13,,,Michael McFarland,22 Foxtail Lane,,,,
B108,,2011-02-16 06:18:35,,,Michael Camacho,6501 Overheart Ln,,,,
123,,2011-02-16 06:17:00,,,Robert A. Guffin,Greens Farms Academy,35 Beachside Avenue,,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,


0
 
sjklein42Commented:
I have changed it to ignore the header line, and it also will show debugging output when it opens the output files.

Be sure the output folder exists before running, or it will fail:

c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\

Please try it and post the results.

#!/usr/bin/perl5 


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

$headerLine = <IN>;	# ignore header line
my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";
print STDERR "->->-> opening file $f\n";

open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}" 

Open in new window

0
 
Navarre_EDIAuthor Commented:
Hello

Here is the error I got.

I am send you the data files.

C:\Perl\script>mikea.pl
Global symbol "$headerLine" requires explicit package name at C:\Perl\script\mik
ea.pl line 13.
Execution of C:\Perl\script\mikea.pl aborted due to compilation errors.
temp2.csv
0
 
sjklein42Commented:
Sorry, forgot you were running with "strict" PERL syntax.  Try this:

#!/usr/bin/perl5 


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

my $headerLine = <IN>;	# ignore header line
my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";
print STDERR "->->-> opening file $f\n";

open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}" 

Open in new window

0
 
Navarre_EDIAuthor Commented:
Hello

Still get a problem at line open OUT,">>$f" or die $!;
but it looks like the output is correct.

C:\Perl\script>mikea.pl
Begin the program:
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\47956856.AUDsi
nglepo
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\ABC.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\CFD.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\111.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\111.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\
.AUDsinglepo
Invalid argument at C:\Perl\script\mikea.pl line 24.
0
 
Navarre_EDIAuthor Commented:
I think my format is incorrect in the layoput of the flat file
0
 
sjklein42Commented:
I made a change so it will ignore blank lines

#!/usr/bin/perl5 


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

my $headerLine = <IN>;	# ignore header line
my @lines = <IN>;

close IN;

for (@lines)
{
s/[\r\n]//g;
/^([^\,]+)/ || next;

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";
print STDERR "->->-> opening file $f\n";

open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}" 

Open in new window

0
 
Navarre_EDIAuthor Commented:
When ran this script it work but with the sample below it only add one line for 86665 when there should have been two


PONUMBER,ORDERREFID,ORDERDATE,REQUESTEDDELIVERYDATE,ORDERCANCELDATE,SHIPPING_NAME,SHIPPING_STREET1,SHIPPING_STREET2,SHIPPING_APTSUITEPOBOX,SHIPPING_ATTN,SHIPPING_CITY,SHIPPING_STATEREGION,SHIPPING_POSTALCODE,SHIPPING_COUNTRY,SHIPPING_CONTACTPHONE,SHIPPING_EMAILADDRESS,SHIPPINGMETHOD,SHIPPINGCARRIER,LINENUMBER,LINEITEMREFID,CUSTOMERITEMNUMBER,UPCNUMBER,NAVARREITEMNUMBER,SHORTDESCRIPTION,QUANTITY,UOM,UNITPRICE,LINEITEMPRICE
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
4665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
Aqwqq,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
0
 
sjklein42Commented:
Works for me (naturally).  :)

Please post the "logging" output you get when you run the program.  Do you get two "86665" lines like I did?  If you made any changes at all to the code, please post the exact PERL file source code as it stands now.

c:\temp>perl foo.pl foo.dat
Begin the program:
->->-> opening file c:\temp\86665.AUDsinglepo
->->-> opening file c:\temp\86665.AUDsinglepo
->->-> opening file c:\temp\4665.AUDsinglepo
->->-> opening file c:\temp\665.AUDsinglepo
->->-> opening file c:\temp\Aqwqq.AUDsinglepo

c:\temp>type 86665.AUDsinglepo
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,85304
1002027,8064053,SOMA Dock,1,EA,399,399
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,85304
1002027,8064053,SOMA Dock,1,EA,399,399

c:\temp>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now