D
dgp
I'm new to Perl, and would like any advice/input on the following
script. It is written to open a file provided by a command-line
argument, seach for "SECTION" header, grab the next 112 lines into an
array, then write out the array in a specific order. It seems to work,
but I'm looking for comments on how it might be improved. I'd
specifically like to do the following:
1) Remove leading and trailing whitespace from the lines that are
"pushed" onto @points. I can't seem to figure it out.
2) I borrowed the scalar<INF> for 1..112 from another topic in this
group. What is this doing? Is there a clearer way to grab the 112 lines
following the "SECTION" header?
3) I'd like check that a command line argument was provided, if not,
prompt the user for $infile.
#!C:\Perl\bin\perl.exe
# rpt2ibl.pl
# Generates ProE IBL File from BladeRunner RPT File
# Requires input filename as commandline argument.
# Example: rpt2ibl.pl inputfile.rpt
use warnings;
$infile = $ARGV[0]; #Get input filename from command line argument.
$outfile = $infile;
$outfile =~ s/\.[^.]+$/.ibl/; #Change output filename extension to ibl.
open (INF, "<$infile") or die "Cannot open $infile for read.\n$!\n";
open (OUTF, ">$outfile") or die "Cannot open $outfile for
write.\n$!\n";
print OUTF "Closed Index Pointwise\n\n"; #Write File Header
while (<INF>) { #Reads each line into $_
if ($_ =~ /SECTION\s\w-\w/) {
++$section; #Increment Section Number
@points=''; #Clear points array
push @points, scalar<INF> for 1..112; #Read points
print OUTF "Begin section ! $section\n"; #Write Section Header
print OUTF "\tBegin curve ! 1\n"; #Write curve header
print OUTF "@points[105..112]";
print OUTF "@points[1..9]";
print OUTF "\tBegin curve ! 2\n"; #Write curve header
print OUTF "@points[9..49]";
print OUTF "\tBegin curve ! 3\n"; #Write curve header
print OUTF "@points[49..65]";
print OUTF "\tBegin curve ! 4\n"; #Write curve header
print OUTF "@points[65..105]";
}
}
close INF;
close OUTF;
Thank you for any help you can provide.
Dave
script. It is written to open a file provided by a command-line
argument, seach for "SECTION" header, grab the next 112 lines into an
array, then write out the array in a specific order. It seems to work,
but I'm looking for comments on how it might be improved. I'd
specifically like to do the following:
1) Remove leading and trailing whitespace from the lines that are
"pushed" onto @points. I can't seem to figure it out.
2) I borrowed the scalar<INF> for 1..112 from another topic in this
group. What is this doing? Is there a clearer way to grab the 112 lines
following the "SECTION" header?
3) I'd like check that a command line argument was provided, if not,
prompt the user for $infile.
#!C:\Perl\bin\perl.exe
# rpt2ibl.pl
# Generates ProE IBL File from BladeRunner RPT File
# Requires input filename as commandline argument.
# Example: rpt2ibl.pl inputfile.rpt
use warnings;
$infile = $ARGV[0]; #Get input filename from command line argument.
$outfile = $infile;
$outfile =~ s/\.[^.]+$/.ibl/; #Change output filename extension to ibl.
open (INF, "<$infile") or die "Cannot open $infile for read.\n$!\n";
open (OUTF, ">$outfile") or die "Cannot open $outfile for
write.\n$!\n";
print OUTF "Closed Index Pointwise\n\n"; #Write File Header
while (<INF>) { #Reads each line into $_
if ($_ =~ /SECTION\s\w-\w/) {
++$section; #Increment Section Number
@points=''; #Clear points array
push @points, scalar<INF> for 1..112; #Read points
print OUTF "Begin section ! $section\n"; #Write Section Header
print OUTF "\tBegin curve ! 1\n"; #Write curve header
print OUTF "@points[105..112]";
print OUTF "@points[1..9]";
print OUTF "\tBegin curve ! 2\n"; #Write curve header
print OUTF "@points[9..49]";
print OUTF "\tBegin curve ! 3\n"; #Write curve header
print OUTF "@points[49..65]";
print OUTF "\tBegin curve ! 4\n"; #Write curve header
print OUTF "@points[65..105]";
}
}
close INF;
close OUTF;
Thank you for any help you can provide.
Dave