For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > Extra Commas in output!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Extra Commas in output!
David Gilden

2007-12-06, 8:00 am


This is adding to many ',' in my output and do not see why!

Could some one could point where I have gone wrong.
Thanks,

Dave Gilden=20
(kora musician / audiophile / webmaster @ www.coraconnection.com / Ft. Wor=
th, TX, USA)



data in: (one row of many)
35, 'Mike Hoffman', '', '2565 Davis Blvd. #177', '', 'Ft. Worth', '75252', =
'Texas', 'United States', '972267-2820', 'test@sbcglobal.net', 2, 'Mike Hof=
fman', '', '2565 Davis Blvd. #177', '', 'Ft.', '75252', 'Texas', 'United St=
ates', 2, 'Richard Hoffman', '', '6565 Davis Blvd. #177', '', 'Dallas', '75=
252', 'Texas', 'United States', 2, 'Authorize.net AIM', 'Visa', 'Richard Ho=
ffman', '5446731215700551', '0807', '2007-07-03 15:12:30', '2007-07-02 19:3=
5:44', 3, NULL, 'USD', 1.000000, NULL

output #not correct # note the end of this string
(35, 'Mike Hoffman', '', '2565 Davis Blvd. #177', '', 'Ft. worth', '75252',=
'Texas', 'United States', '972267-2820', 'test@sbcglobal.net', 2, 'Mike Ho=
ffman', '', '2565 Davis Blvd. #177', '', 'Ft. worth', '75252', 'Texas', 'Un=
ited States', 2, 'Mike Hoffman', '', '2565 Davis Blvd. #177', '', 'Ft. wort=
h', '75252', 'Texas', 'United States', 2, 'Authorize.net AIM', 'Visa', 'Mik=
e Hoffman', '5446731215700551', '0809', '2007-07-08 21:30:10', '2007-07-05 =
16:01:48', 3, NULL, 'USD', 1.000000, NULL,'','','','','','','','','','','',=
'','','','','','','','','','','','','','
','','','','','',,,'',,'','','','',=
,'','','','','','',,,,)

# note the end of this string
# why am I getting the extra [,] s

-----

#!/usr/bin/perl -w

$fileIn =3D "orders-raw.txt" ;
$CleanCSV =3D "clean_orders_insert.txt";


####
open (FH, $fileIn) or &errorMsg("Problem reading $fileIn $!\n");

while(<FH> ){
push(@record, $_);=20
}=20
close FH;



sub errorMsg{
$_ =3D shift;
print "Error: $_\n";
}


open(CSV_OUT,">$CleanCSV") or &errorMsg( "Write out, Could not write file:=
$CleanCSV, $!");
=20
foreach (@record){
chomp; =20
#Split each line
my @tmpArray =3D split(/","/,$_);=20
my @tmpArray =3D split(/","/,$_);=20

$customers_id =3D $tmpArray[0] || "''";
$customers_name =3D $tmpArray[1] || "''";
$customers_company =3D $tmpArray[2] || "''";
$customers_street_address =3D $tmpArray[3] || "''";
$customers_suburb =3D $tmpArray[4] || "''";
$customers_city =3D $tmpArray[5] || "''";
$customers_postcode =3D $tmpArray[6] || "''";
$customers_state =3D $tmpArray[7] || "''";
$customers_country =3D $tmpArray[8] || "''";
$customers_telephone =3D $tmpArray[9] || "''";
$customers_email_address =3D $tmpArray[10] || "''";
$customers_address_format_id =3D $tmpArray[11] || "''";
$delivery_name =3D $tmpArray[12] || "''";
$delivery_company =3D $tmpArray[13] || "''";
$delivery_street_address =3D $tmpArray[14] || "''";
$delivery_suburb =3D $tmpArray[15] || "''";
$delivery_city =3D $tmpArray[16] || "''";
$delivery_postcode =3D $tmpArray[17] || "''";
$delivery_state =3D $tmpArray[18] || "''";
$delivery_country =3D $tmpArray[19] || "''";
$delivery_address_format_id =3D $tmpArray[20] || "''";
$billing_name =3D $tmpArray[21] || "''";
$billing_company =3D $tmpArray[22] || "''";
$billing_street_address =3D $tmpArray[23] || "''";
$billing_suburb =3D $tmpArray[24] || "''";
$billing_city =3D $tmpArray[25] || "''";
$billing_postcode =3D $tmpArray[26] || "''";
$billing_state =3D $tmpArray[27] || "''";
$billing_country =3D $tmpArray[28] || "''";
$billing_address_format_id =3D $tmpArray[29] || "''";
$payment_method =3D $tmpArray[30] || "''";
$cc_type =3D $tmpArray[31] || "''";
$cc_owner =3D $tmpArray[32] || "''";
$cc_number =3D $tmpArray[33] || "''";
$cc_expires =3D $tmpArray[34] || "''";
$last_modified =3D $tmpArray[35] || "''";
$date_purchased =3D $tmpArray[36] || "''";
$orders_status =3D $tmpArray[37] || "''";
$orders_date_finished =3D $tmpArray[38] || "''";
$currency =3D $tmpArray[39] || "''";
$currency_value =3D $tmpArray[40] || "''";
$shipping_method =3D $tmpArray[41] || "''"; #shipping_module
# Write TMP file=20
print CSV_OUT " ($customers_id,$customers_name,$customer
s_company,$customers=
_street_address,$customers_suburb,$custo
mers_city,$customers_postcode,$cust=
omers_state,$customers_country,$customer
s_telephone,$customers_email_addres=
s,$customers_address_format_id,$delivery
_name,$delivery_company,$delivery_s=
treet_address,$delivery_suburb,$delivery
_city,$delivery_postcode,$delivery_=
state,$delivery_country,$delivery_addres
s_format_id,$billing_name,$billing_=
company,$billing_street_address,$billing
_suburb,$billing_city,$billing_post=
code,$billing_state,$billing_country,$bi
lling_address_format_id,$payment_me=
thod,$payment_module_code,$shipping_modu
le,$shipping_method,$coupon_code,$c=
c_type,$cc_owner,$cc_number,$cc_expires,
$cc_cvv,$last_modified,$date_purcha=
sed,$orders_status,$orders_date_finished
,$currency,$currency_value,$order_t=
otal,$order_tax,$paypal_ipn_id,$ip_addre
ss)\n";

}

close(CSV_OUT);

Paul Lalli

2007-12-06, 7:02 pm

On Dec 6, 12:49 am, do...@coraconnection.com (David Gilden) wrote:
> This is adding to many ',' in my output and do not see why!
>
> Could some one could point where I have gone wrong.


Well for one, by posting code that you're not actually running, so
people can't actually help you.

> data in: (one row of many)
> 35, 'Mike Hoffman', '', '2565 Davis Blvd. #177', '', 'Ft. Worth', '75252', 'Texas', 'United States', '972267-2820', 't...@sbcglobal.net', 2, 'Mike Hoffman', '', '2565 Davis Blvd. #177', '', 'Ft.', '75252', 'Texas', 'United States', 2, 'Richard Hoffman',

'', '6565 Davis Blvd. #177', '', 'Dallas', '75252', 'Texas', 'United States', 2, 'Authorize.net AIM', 'Visa', 'Richard Hoffman', '5446731215700551', '0807', '2007-07-03 15:12:30', '2007-07-02 19:35:44', 3, NULL, 'USD', 1.000000, NULL
>
> output #not correct # note the end of this string
> (35, 'Mike Hoffman', '', '2565 Davis Blvd. #177', '', 'Ft. worth', '75252', 'Texas', 'United States', '972267-2820', 't...@sbcglobal.net', 2, 'Mike Hoffman', '', '2565 Davis Blvd. #177', '', 'Ft. worth', '75252', 'Texas', 'United States', 2, 'Mike Hoffm

an', '', '2565 Davis Blvd. #177', '', 'Ft. worth', '75252', 'Texas', 'United States', 2, 'Authorize.net AIM', 'Visa', 'Mike Hoffman', '5446731215700551', '0809', '2007-07-08 21:30:10', '2007-07-05 16:01:48', 3, NULL, 'USD', 1.000000, NULL,'','','','','','
','','','','','','','','','','','','',''
,'','','','','- ','','','','','','',,,'',,'','','','',,'
','','','','','',,,,)

> #!/usr/bin/perl -w
>
> $fileIn = "orders-raw.txt" ;
> $CleanCSV = "clean_orders_insert.txt";
>
> ####
> open (FH, $fileIn) or &errorMsg("Problem reading $fileIn $!\n");
>
> while(<FH> ){
> push(@record, $_);}
>
> close FH;
>
> sub errorMsg{
> $_ = shift;
> print "Error: $_\n";
>
> }
>
> open(CSV_OUT,">$CleanCSV") or &errorMsg( "Write out, Could not write file: $CleanCSV, $!");
>
> foreach (@record){
> chomp;
> #Split each line
> my @tmpArray = split(/","/,$_);
> my @tmpArray = split(/","/,$_);


Ignoring for the moment that you're doing the same exact command
twice, including declaring the variable, look what you're splitting
on. You're splitting on a double quote followed by a comma followed
by a double quote. Your data above is not separated by a doublequote
followed by a comma followed by a double quote. Your fields are
separated by commas followed by spaces. Therefore, your @tmpArray
contains exactly one element - the entire string. But then you're
going ahead and assuming there are 42 elements in the array, and
trying to assign variables to all 42 of them, and then printing out 42
different variables.

<snip>

> print CSV_OUT " ($customers_id,$customers_name,$customer
s_company,$customers_street_addres- s,$customers_suburb,$customers_city,$cus
tomers_postcode,$customers_state,$c- ustomers_country,$customers_telephone,$c
ustomers_email_address,$customers_a-ddress_form

at_id,$delivery_name,$delivery_company,$
delivery_street_address,- $delivery_suburb,$delivery_city,$deliver
y_postcode,$delivery_state,$deliver- y_country,$delivery_address_format_id,$b
illing_name,$billing_company,$billi-ng_street_address,$billing_suburb,$bil
ling_city,$billing_postcode,$billing_- state,$billing_country,$billing_address_
format_id,$payment_method,$payment_- module_code,$shipping_module,$shipping_m
ethod,$coupon_code,$cc_type,$cc_own- er,$cc_number,$cc_expires,$cc_cvv,$last_
modified,$date_purchased,
$orders_st- atus,$orders_date_finished,$currency,$cu
rrency_value,$order_total,$order_ta-x,$paypal_ipn_id,$ip_address)\n";

I'm not looking at all of this mess, but where the hell did
$ip_address and $paypal_ipn_id come from? They're not listed among
your 42 variables. Post code that you're actually running.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com