Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie Looking for Advice/Comments on Script

Reply
Thread Tools

Newbie Looking for Advice/Comments on Script

 
 
dgp@dodgeit.com
Guest
Posts: n/a
 
      02-02-2005
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      02-02-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I'm new to Perl


http://learn.perl.org is an excellent place to start

>, 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.


Please check the Perl FAQ *before* posting:
perldoc -q space
"How do I strip blank space from the beginning/end of a string?"


> 2) I borrowed the scalar<INF> for 1..112 from another topic in this
> group. What is this doing?


the actual line:
push @points, scalar<INF> for 1..112;
is syntactic sugar for:
for (1..112) {
push @points, scalar<INF>;
}
In a scalar context, <INF> will return the 'next' line from the opened
file. So you are adding the next line of the file to @points 112 times.
The 'scalar' is necessary because otherwise <INF> would be evaluated in
a list context, which would cause it to return ALL remaining lines in
the file.

> Is there a clearer way to grab the 112 lines following the "SECTION"

header?

You could do a looping construct, taking note of the $. variable, which
holds the current input file line number, and break out of the loop when
$. is 112 more than it was when you found "SECTION".

> 3) I'd like check that a command line argument was provided, if not,
> prompt the user for $infile.


Command line arguments are stored in @ARGV. If @ARGV is empty, there
were no arguments provided.

> #!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 strict;
Always use strictures. They help prevent errors. After adding this
line, you will have to declare your variables with 'my'. This is a good
thing.

> use warnings;


Good!!

> $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";


Most people prefer lexical filehandle references these days instead of
the all-caps barewords
open my $inf, '<', $infile or die "Cannot open $infile: $!";
(similar for OUTF)
>
> print OUTF "Closed Index Pointwise\n\n"; #Write File Header
>
> while (<INF>) { #Reads each line into $_
> if ($_ =~ /SECTION\s\w-\w/) {


pattern matches default to $_. There is no reason to make it explicit.
(ie, remove "$_ =~ ")

> ++$section; #Increment Section Number


Your formatting is atrocious. I'm guessing this is due to the method
with which you have posted this to usenet. I'm also guessing you used
Google Groups to do so. Please don't. Google Groups has been
contacted - by many people - about their senseless stripping of
whitespace, and have thus far not responded. Until they do, use a real
newsreader.

> @points=''; #Clear points array


This does not clear the @points array. This sets the @points array to
contain exactly one element, the empty string. To clear an array, do
@points = ();

However, this should be unnecessary, as you should be declaring @points
lexically at this stage. Change this line to
my @points;
and you will get a fresh copy of the lexical array @points for each
iteration of the loop.

> 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]";


You are adding extra spaces in front of each line. You are doing this
because you've chosen to interpolate the array slice. Interpolated
arrays and slices are separated by the $" variable, which defaults to a
single space. To fix this, either locally set $" to the empty string:
local $" = '';

or, preferred, don't interpolate the slices. Just print them out:
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.


Hope this is helpful to you.

Paul Lalli.

P.S. I will preemptively say this: If you choose to reply, please quote
what you are replying to. This newsgroup has seen a rash of Google
Groups posters who choose to reply without quoting anything. Only a
small fraction of the readers of this newsgroup use Google Groups to
read Usenet, for various and valid reasons. Thank you.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [SI] Reminder, Looking Up/Looking Down is due March 17th. Tony Cooper Digital Photography 9 03-17-2013 05:03 AM
How to execute a script from another script and other script does notdo busy wait. Rajat Python 3 01-08-2010 02:05 PM
RE: How to execute a script from another script and other script doesnotdo busy wait. VYAS ASHISH M-NTB837 Python 2 01-07-2010 08:18 PM
Looking for a Ruby Programmer Looking for a Fun Project. ;) Mark Ruby 9 03-10-2006 08:11 PM
Newbie: Looking for comments on this (working) script Trebor A. Rude Perl Misc 14 04-28-2004 08:02 PM



Advertisments