in reply to Re^2: How to have perl check line length
in thread How to have perl check line length

You are most welcome, but I do advise that if you are going to maintain Perl code, it would be wise to examine the much more efficient mechanisms shown by other authors. Mine was designed to be readable; it is not exactly the most efficient way to do it in Perl. :-)

That said, glad we could help.

  • Comment on Re^3: How to have perl check line length

Replies are listed 'Best First'.
Re^4: How to have perl check line length
by ddrew78 (Beadle) on Sep 18, 2013 at 01:14 UTC
    Apparently the code my company uses is so old, I tried the other suggestions as well, but couldn't get them to work. Also, I actually ended up modifying your code just a bit, specifically to this:
    open(NPBXNUM1, ">npbxnum1"); open(MYINPUTFILE, "npbxnum"); while (<MYINPUTFILE>) { my($line) = $_; chomp($line); foreach my $inputData ($line) { my $outputData = $inputData; while (length($outputData) < 4) { $outputData .= '_'; } print NPBXNUM1 "$outputData\n"; } } close(MYINPUTFILE); close(NPBXNUM1);
    Thanks again for the help.
      You're welcome, again -- are you up for a little code analysis? There are a few things in the script as you've modified it which really ought to be adjusted if you have the time.

      1. open(NPBXNUM1, ">npbxnum1"); open(MYINPUTFILE, "npbxnum");
        You went through the effort of specifying > on the output file; consistency has much value in engineering. Why not boldly include the < on the input file?
        open(NPBXNUM1, ">npbxnum1"); open(MYINPUTFILE, "<npbxnum");
        And I'm sure other Monks would strongly recommend breaking the < and > out into their own parameters, for reasons I don't pretend to understand:
        open(NPBXNUM1, '>', "npbxnum1"); open(MYINPUTFILE, '<', "npbxnum");
      2. while (<MYINPUTFILE>) { my($line) = $_; chomp($line);
        probably better as:
        while (my $line = <MYINPUTFILE>) { chomp($line);
      3. foreach my $inputData ($line) { my $outputData = $inputData; while (length($outputData) < 4) { $outputData .= '_'; } print NPBXNUM1 "$outputData\n"; }
        The foreach loop is pointless, as its purpose is to iterate through an array; $line is a scalar.

        Perhaps better as:

        my $outputData = $line; while (length($outputData) < 4) { $outputData .= '_'; } print NPBXNUM1 "$outputData\n"; }