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:
output csv data to table?

 



velixer
New User

Dec 14, 2012, 9:40 AM

Post #1 of 5 (1387 views)
output csv data to table? Can't Post

Hi guys.

Thought about using perl to take some csv data (PLUTemp.plu_code[7][2] etc are the column headings) than output it such:


Quote
<0000>
PLUTemp.plu_code[7][2]=00000000000001
PLUTemp.plu_caption[2]=00
PLUTemp.plu_price[4][2]=00014100
PLUTemp.plu_name[20][2]=A |
PLUTemp.plu_inprice[4][2]=00000100
PLUTemp.plu_store[5][2]=FF00000100
PLUTemp.plu_sale[5][2]=0000000300
PLUTemp.plu_money[5][2]=0000042300
<0001>
PLUTemp.plu_code[7][2]=00000000000002
PLUTemp.plu_caption[2]=00
PLUTemp.plu_price[4][2]=00078600
PLUTemp.plu_name[20][2]=B |
PLUTemp.plu_inprice[4][2]=00000500
PLUTemp.plu_store[5][2]=0000000400
PLUTemp.plu_sale[5][2]=0000000000
PLUTemp.plu_money[5][2]=0000000000
<0002>
PLUTemp.plu_code[7][2]=00000000000003
PLUTemp.plu_caption[2]=00
PLUTemp.plu_price[4][2]=00006900
PLUTemp.plu_name[20][2]=C |
PLUTemp.plu_inprice[4][2]=00000600
PLUTemp.plu_store[5][2]=0000000200
PLUTemp.plu_sale[5][2]=0000000000
PLUTemp.plu_money[5][2]=0000000000
<0003>
PLUTemp.plu_code[7][2]=00000000000010
PLUTemp.plu_caption[2]=00
PLUTemp.plu_price[4][2]=00000099
PLUTemp.plu_name[20][2]=CHIPS |
PLUTemp.plu_inprice[4][2]=00000020
PLUTemp.plu_store[5][2]=0000009700
PLUTemp.plu_sale[5][2]=0000000200
PLUTemp.plu_money[5][2]=0000000198


saw text::csv.

can somebody talk me through how to do this?


Laurent_R
Veteran / Moderator

Dec 14, 2012, 9:54 AM

Post #2 of 5 (1384 views)
Re: [velixer] output csv data to table? [In reply to] Can't Post

If what you provided is the output format, can you please provide the input format?

You did not provide enough information to enable us to help us.


velixer
New User

Dec 14, 2012, 10:09 AM

Post #3 of 5 (1382 views)
Re: [Laurent_R] output csv data to table? [In reply to] Can't Post

Thanks for the quick reply Laurent,

Here is the input format from the csv


Quote
,PLUTemp.plu_code[7][2]=,PLUTemp.plu_caption[2]=,PLUTemp.plu_price[4][2]=,PLUTemp.plu_name[20][2]=,PLUTemp.plu_inprice[4][2]=,PLUTemp.plu_store[5][2]=,PLUTemp.plu_sale[5][2]=,PLUTemp.plu_money[5][2]=
<0000> ,00000000000001 ,00 ,00014100 ,A | ,00000100 ,FF00000100 ,0000000300 ,0000042300
<0001> ,00000000000002,00 ,00078600,B ,00000500,0000000400,0000000300 ,0000042300



Laurent_R
Veteran / Moderator

Dec 14, 2012, 10:48 AM

Post #4 of 5 (1379 views)
Re: [velixer] output csv data to table? [In reply to] Can't Post

Hi Velixer,

a quick untested program to do (more or less) what you want:


Code
while (my $line = <$INPUT_FILE>) { 
chomp $line;
if ($line =~ /PLUTemp/ ) {
my @codes = split /,/, $line;
}
else {
my @numbers = split /,/, $line;
my $main_code = shift @numbers;
print $main_code, "\n";
while (my $code = shift @codes) {
my $number = shift @numbers;
print $code, $number, "\n";
}
}
}



velixer
New User

Dec 15, 2012, 4:07 AM

Post #5 of 5 (1374 views)
Re: [Laurent_R] output csv data to table? [In reply to] Can't Post

fandabidastic mate!

cheers again for the help

here is the code I ended up using


Quote
print ("starting program","\n");
print ("\n");
open INPUT_FILE, "<", "inputplu.csv" or die $!;

open OUT_FILE, ">", "hucr.txt" or die $!;

while (my $line = <INPUT_FILE>) {

chomp $line;


if ($line =~ /PLUTemp/ ) {
my @codes = split /,/, $line;

}
else {
my @numbers = split /,/, $line;

open OUT_FILE, ">>", "hucr.txt" or die $!;

my $main_code = shift @numbers;
print OUT_FILE ($main_code, "\n");

my $main_code = shift @numbers;
print OUT_FILE ("PLUTemp.plu_code[7][2]=",

$main_code, "\n");

my $main_code = shift @numbers;
print OUT_FILE ("PLUTemp.plu_caption[2]=",

$main_code, "\n");

my $main_code = shift @numbers;
print OUT_FILE ("PLUTemp.plu_price[4][2]=",

$main_code, "\n");

my $main_code = shift @numbers;
print OUT_FILE ("PPLUTemp.plu_name[20][2]

=", $main_code, "\n");

my $main_code = shift @numbers;
print OUT_FILE ("PLUTemp.plu_inprice[4][2]

=", $main_code, "\n");

my $main_code = shift @numbers;
print OUT_FILE ("PLUTemp.plu_store[5][2]=",

$main_code, "\n");


my $main_code = shift @numbers;
print OUT_FILE ("PLUTemp.plu_sale[5][2]=",

$main_code, "\n");


my $main_code = shift @numbers;
print OUT_FILE ("PLUTemp.plu_money[5][2]=",

$main_code, "\n");

close OUT_FILE;


while (my $code = shift @codes) {
my $number = shift @numbers;
print $code, $number, "\n";
}
}
}


 
 


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

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