Joseph L. Casale wrote:
From: Rob Dixon
Joseph L. Casale wrote:
One of these scripts has a loop like this:
for my $line (@lines){
my $line2 = $line;
$line =~ s/(\S+)\s+(\S+)\s+(\S+)/X$1 Y$2/;
print FILEOUT $line;
$line2 =~ s/(\S+)\s+(\S+)\s+(\S+)/Z[$3+DPad]/;
print FILEOUT $line2;
print FILEOUT "M98PDRILL.SUBL1\n";
print FILEOUT "G90\n";
print FILEOUT "G00 Z[CPlane]\n"
}
What would be a wise way of trapping a condition such as the line
read and passed into the loop is not 3 sets of numbers and if so,
skip?
It's also worth pointing out that Paul originally loaded the file
data into an array so that he could use it twice in two successive
loops. Unless the amount of your data is tiny it's much better to
remove the array @lines and write this loop as
while (<DATA>) {
:
}
which reads the file one line at a time and doesn't need to draw it all
into memory at once.
OK, I saw your example and noted it. I intended on using next time as
I know there will be:) But now I am convinced, as the lack of error
checking in my script worries me. I'll take yours and fit it in!
I do need to read up on what you're doing as I am not clear on its
syntax in this email. I am using this:
if ($#ARGV != 1) {
print "usage: ConvertASCII.pl \"input file name\" \"output file name\"\n";
exit;
}
open (FILEIN, "< $ARGV[0]") or die $!;
my @lines = <FILEIN>;
open (FILEOUT, "> $ARGV[1]") or die $!;
So using your syntax escapes me at the moment:)
Hi Joseph
(Please bottom-post your responses to this list, so that longer threads remain
readable. Thank you.)
So your complete script should look like the program below. I wouldn't write it
exactly like this, but I've changed a minimum of your code so that you can see
where it's come from.
One thing I have changed is your usage check. An array in scalar context
provides
the number of elements it has, whereas the $#ARGV you used gives the index of
the
last element. Together with 'unless' instead of 'if' I believe it makes this
clause more readable.
Post again if there's anything else you don't understand.
HTH,
Rob
use strict;
use warnings;
unless (@ARGV == 2) {
print "usage: ConvertASCII.pl \"input file name\" \"output file name\"\n";
exit;
}
open (FILEIN, "< $ARGV[0]") or die $!;
open (FILEOUT, "> $ARGV[1]") or die $!;
while (<FILEIN>) {
my @data = split;
next unless @data == 3;
next if grep /[^0-9.-]/, @data;
printf FILEOUT "X%s Y%s\n", $data[0], $data[1];
printf FILEOUT "Z[%s+DPad]\n", $data[2];
print FILEOUT "M98PDRILL.SUBL1\n";
print FILEOUT "G90\n";
print FILEOUT "G00 Z[CPlane]\n"
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/