Hi,
I am new to using Perl to parse a file. What I am trying to do is parse a file that
is made of online orders. I have written some code using for and if statements that
will go through the file line by line and using regex I try to match the information
with the variables so that I can write it to a MySQL database. This works fine as
long as each order has their information where it is supposed to be? Is there an
easier way of doing this? I get half way through processing the file and the arrays
turn to jibberish! I have included my script if it helps any. TIA
Trevor
open(Order, "<c:\\maverick\\test2.TXT") || die ("Cannot find the file test2.TXT");
flock(Order,2);
@miva_orders=<Order>;
@order_number = ();
@date = ();
@time = ();
@bill_first_name = ();
@bill_last_name = ();
@ship_first_name = ();
@ship_last_name = ();
@emai_address = ();
@phone_number = ();
@phone_number2 = ();
@business_name = ();
@sold_to_street = ();
@bill_to_street = ();
@bill_to_city = ();
@bill_to_state = ();
@bill_to_zip = ();
@ship_to_city = ();
@ship_to_state = ();
@ship_to_zip = ();
@contry = ();
@code = ();
@name = ();
@quantity = ();
@price_each = ();
@shipping_method = ();
@shipping_amount = ();
#shift @miva_orders;
#shift @miva_orders;
print "Arrya[0] is: $miva_orders[0]\n";
[EMAIL PROTECTED];
print "$length\n";
for ($n=1; $n<$length; $n=$n+26) {
print "N : $n and value of $miva_orders[$n]\n";
if ($order_number[$n] eq ""){
if ($miva_orders[$n+3] =~ /^Order Number\s?:\s?(\d+)/) {
#$order_number = $1;
$order_number[$n] = $1;
#push(@order_number,$order_number);
print "Order number is $order_number[$n]\n";
}
}
if ($date[$n] eq "" && $time[$n] eq "" ) {
if ($miva_orders[$n+4] =~ m/Placed\s+: (\d+\/\d+\/\d+)\s(\d+:\d+:\d+)/) {
$date[$n] = $1;
$time[$n] = $2;
print "Date and Time is $1 $2\n";
}
}
if ($bill_first_name[$n] eq "" && $bill_last_name[$n] eq "" && $ship_first_name[$n]
eq "" && $ship_last_name[$n] eq "") {
if($miva_orders[$n+7] =~ /^(\w+)\s(\w+)\s+(\w+)?\s?(\w+)?/) {
$ship_first_name[$n] = $1;
$ship_last_name[$n] = $2;
$bill_first_name[$n] = $3;
$bill_last_name[$n] = $4;
print "Ship First name : $ship_first_name[$n]\n";
print "Ship Last name : $ship_last_name[$n]\n";
print "Bill Last name : $bill_first_name[$n] \n";
print "Bill Last name : $bill_last_name[$n] \n";
}
}
if ($email_address[$n] eq "") {
if($miva_orders[$n+8]=~ /([EMAIL PROTECTED])/) {
$email_address[$n] = $1;
print "Email address : $email_address[$n]\n";
}
}
if( $phone_number[$n] eq "") {
if($miva_orders[$n+9] =~ /(\d{3}\s?-?\d{3}\s?-?\d{4})/) {
$phone_number[$n]=$1;
print "Phone Number $phone_number[$n]\n";
}
}
if( $phone_number2[$n] eq "") {
if($miva_orders[$n+10] =~ / \s+(\d{3}\s?-?\d{3}\s?-?\d{4})? / ) {
$phone_number2[$n]=$1;
print "Phone Number2 $phone_number2[$n]\n";
}
}
if($business_name[$n] eq "" ) {
if ($miva_orders[$n+11] =~ /^\s+(\w+.*)/) {
$business_name[$n] = $1;
$business_name[$n] =~ s/^\s+//;
print "Business Name $business_name[$n]\n";
}
}
if ($sold_to_street[$n] eq "") {
if($miva_orders[$n+12] =~ /(.*)\s{2,}/) {
$sold_to_street[$n] = $1;
print "Sold To Street :$sold_to_street[$n] \n";
}
}
if ($bill_to_street[$n] eq "") {
if($miva_orders[$n+12] =~ /\s{2,}(.*)/) {
$bill_to_street[$n] = $1;
print "Bill To Street :$bill_to_street[$n] \n";
}
}
if ($bill_to_city[$n] eq "" && $bill_to_state[$n] eq "" && $bill_to_zip[$n] eq "") {
if ($miva_orders[$n+13] =~ /(\w+\s?\w+)\s?(.{2})\s?(\d{5}(\W)?(\d{4})?)/) {
$bill_to_city[$n] = $1;
$bill_to_state[$n] = $2;
$bill_to_zip[$n] = $3;
print "Bill to City, State and Zip : $bill_to_city[$n]
$bill_to_state[$n] $bill_to_zip[$n] \n";
}
}
if ($ship_to_city[$n] eq "" && $ship_to_state[$n] eq "" && $ship_to_zip[$n] eq "") {
if($miva_orders[$n+13] =~ /\s{2,}(\w+\s?\w+)\s?(\w{2})\s?(\d{5}(\W)?(\d{4})?)/) {
$ship_to_city[$n] = $1;
$ship_to_state[$n] = $2;
$ship_to_zip[$n] = $3;
print "Ship TO City, State and Zip : $ship_to_city[$n]
$ship_to_state[$n] $ship_to_zip[$n] \n";
}
}
if ($bill_to_country[$n] eq "") {
if ($miva_orders[$n+14] =~ /(\w+)/) {
$bill_to_country[$n] = $1;
print "Country $bill_to_country[$n] \n";
}
}
if ($code[$n] eq "") {
if ($miva_orders[$n+18] =~ /(.{1,15})/) {
$code[$n] = $1;
print "Code : $code[$n]\n";
}
}
if ($name[$n] eq "") {
if ($miva_orders[$n+18] =~ /(\s{2,}.{2,40})/) {
$name[$n] = $1;
$name[$n] =~ s/^\s+//;
print "Name : $name[$n]\n";
}
}
if ($quantity[$n] eq "") {
if ($miva_orders[$n+18] =~ /(\s{2,}\d{1,8})/) {
$quantity[$n] = $1;
$quantity[$n] =~ s/^\s+//;
print "Quantity : $quantity[$n]\n";
}
}
if ($price_each[$n] eq "") {
if ($miva_orders[$n+18] =~ /\$(\d+\.\d+)/) {
$price_each[$n] = $1;
$price_each[$n] =~ s/^\s+//;
print "Price Each : $price_each[$n]\n";
}
}
if ($shipping_method[$n] eq "" && $shipping_amount[$n] eq "" ) {
if ($miva_orders[$n+19] =~ /\s+Shipping:\s+(\w+\s?\w+):\s+\$(\d+\.\d+)/) {
$shipping_method[$n] = $1;
$shipping_method[$n] =~ s/^\s+//;
$shipping_amount[$n] = $2;
print "Shipping Method : $shipping_method[$n]\n";
print "Shipping Amount : $shipping_amount[$n]\n";
}
}
} # for loop
flock(order,8);
close(order);