I'm pretty new myself, as such I like to see a little more error checking to
accomodate for mine (or other people's) mistakes, here's one similar to
Rob's but with some additional error checking that you might want, basically
the main difference is that it expects your lines to be formatted like:
dr5digits<TAB>VariableLengthFieldOfDigits<TAB>5digitsFreeFormDescription
* upper or lower case is fine thanks to the i switch at the end of the regex
If the line doesn't look like that it will let you know what line is in
error and exit.
This also prints both to the screen and to an output file:
Screen Output:
print "Writing output to: $OutputFileName:
$PartNum;$Quantity;$Description\n";
File Output:
print OUTF "$PartNum;$Quantity;$Description\n";
I find this to be quite handy when I'm first running a script like this as
it will help me see how things are working real time.
If you want to stop printing to either one, just delete the line or comment
it out with a preceding #.
===============BEGIN COPY PASTE SCRIPT=========================
#!/usr/bin/perl
use strict;
use warnings;
my $InputFileName = "somefile.txt";
my $OutputFileName = "outputfile.txt";
open(INF, $InputFileName) or die "Could not open $InputFileName for reading:
$!\n";
open(OUTF, ">$OutputFileName") or die "Could not open $OutputFileName for
output: $!\n";
print "\n";
while(<INF>) {
if ($_ !~ /^(dr\d{5})(\t)(\d*)(\t)(\d{5})(.*)/i) {
print "Found an incorrectly formatting line at line: $. $_, please
correct this line and try again\n";
exit;
}
else {
my ($OrderNum, $PartNum, $Quantity, $Description) = ($1, $3, $5, $6);
$Quantity += 0;
chomp($Description);
print "Writing output to: $OutputFileName:
$PartNum;$Quantity;$Description\n";
print OUTF "$PartNum;$Quantity;$Description\n";
}
}
print "\n\nAll done! Please see: $OutputFileName for your data!\n";
close(INF);
close(OUTF);
====================END COPY PASTE SCRIPT====================
Hope this helps!
~P
----- Original Message -----
From: "Rod Burgess" <[EMAIL PROTECTED]>
To: <beginners@perl.org>
Sent: Monday, July 17, 2006 1:59 PM
Subject: Newbie Perl Question
I am new to the Perl world and am trying to learn it. A coworker tells me
that Perl will not work for what I am trying to do however, I think Perl
would be a great tool to use and I feel this coworker is wrong.
I have a file that contains several lines all as below:
DR03555{tab} 45600062888{tab} 00008FLAT WASHER
DR03555{tab} 228765329{tab} 00001GASKET
The meaning of the file is
DR03555 = order number
45600062888 = part number
00008 = quantity
FLAT WASHER = Description
The lines all begin with the prefex DR I would like to read this file and
produce the following output:
45600062888;8;FLAT WASHER
228765329;1;GASKET
basiclly I need a file that lists the following:
Part#;Quantity;Description
Is this possible with Perl?
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>