Newbie Looking for Advice/Comments on Script

Discussion in 'Perl Misc' started by dgp@dodgeit.com, Feb 2, 2005.

  1. Guest

    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
     
    , Feb 2, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    <> wrote in message
    news:...
    > 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.
     
    Paul Lalli, Feb 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,135
  2. David Hirschfield

    Newbie to XML-RPC: looking for advice

    David Hirschfield, Jan 14, 2006, in forum: Python
    Replies:
    0
    Views:
    325
    David Hirschfield
    Jan 14, 2006
  3. Monk
    Replies:
    10
    Views:
    1,478
    Michael Wojcik
    Apr 20, 2005
  4. Trebor A. Rude
    Replies:
    14
    Views:
    199
    John W. Krahn
    Apr 28, 2004
  5. Replies:
    4
    Views:
    614
    Dr John Stockton
    Jun 3, 2006
Loading...

Share This Page