ddrew78 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have a small, yet significant issue. I have a rather large script that generates configuration files for network elements. The problem is that these elements have character limitations on several of the parameters, and currently I am manually manipulating output files to ensure we stay within the character limit. Obviously this makes for easy work errors. One of my input files looks like this:
NT8 NT9 NT10 NT11
What I need perl to do is check each line in the file, and if it is three characters, add a '_' at the end, but omit that underscore if it is 4 characters, so it would look like this:
NT8_ NT9_ NT10 NT11
As usual, any and all help is appreciated.

Replies are listed 'Best First'.
Re: How to have perl check line length
by SuicideJunkie (Vicar) on Sep 17, 2013 at 17:25 UTC
    $line .= '_' x (4 - length $line);

    Be sure to chomp off the end of line characters first.

      So I added it like below to my script, but it doesn't work. Am I missing something else?
      open(NPBXNUM1, ">npbxnum1"); open(MYINPUTFILE, "npbxnum"); while (<MYINPUTFILE>) { chomp; $line .= '_' x (4 - length $line); } close(MYINPUTFILE); close(NPBXNUM1); system "mv npbxnum1 npbxnum";

        Replace $line with whatever variable you are storing the line in. (In this case, $_)

        Also, you should probably print the modified lines to the output file at some point inside the loop.

        PS: use warnings; use strict;, and opens in the form of open my $filehandle, '<', $filename or die "I couldn't open the file.  OS says: $!"; will help highlight likely mistakes in your code and illuminate the reasons for any file errors.

        You really, really, really should use strict, especially if you are new to Perl. It will save you from asking embarrassing questions.

        That is to say, being new to Perl, you might still have to ask:

        "What does Global symbol "$line" requires explicit package name mean?"

        But that's a much better question than "why didn't that line of code I stuck in my program work?"

        #!/usr/bin/perl -w use strict; { open(NPBXNUM1, ">npbxnum1"); open(MYINPUTFILE, "npbxnum"); while (<MYINPUTFILE>) { chomp; $line .= '_' x (4 - length $line); } close(MYINPUTFILE); close(NPBXNUM1); system "mv npbxnum1 npbxnum"; } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-09-17@2038-Use-Strict>usestrict.pl Global symbol "$line" requires explicit package name at C:\Steve\Dev\P +erlMonks\P-2013-09-17@2038-Use-Strict\usestrict.pl line 11. Global symbol "$line" requires explicit package name at C:\Steve\Dev\P +erlMonks\P-2013-09-17@2038-Use-Strict\usestrict.pl line 11. Execution of C:\Steve\Dev\PerlMonks\P-2013-09-17@2038-Use-Strict\usest +rict.pl aborted due to compilation errors.
Re: How to have perl check line length
by marinersk (Priest) on Sep 17, 2013 at 18:59 UTC
    Okay, TMTOWTDI time.

    #!/usr/bin/perl -w use strict; my @TestData = ( 'NT8', 'NT9', 'NT10', 'NT11', ); { foreach my $inputData (@TestData) { my $outputData = $inputData; while (length($outputData) < 4) { $outputData .= '_'; } printf "%-4s -> %s\n", $inputData, $outputData; } } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-09-17@2038-Use-Strict>make4len.pl NT8 -> NT8_ NT9 -> NT9_ NT10 -> NT10 NT11 -> NT11
      Thank you, that did the trick.
        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.

        Here's a toy you might find useful:

        C:\Steve\Dev\PerlMonks\P-2013-09-17@2038-Use-Strict>make4sortable.pl a -> a___ NTX -> NTX_ NT8 -> NT08 NT9 -> NT09 NT10 -> NT10 NT11 -> NT11
Re: How to have perl check line length
by Kenosis (Priest) on Sep 17, 2013 at 17:56 UTC

    Here's another option that will append a "_" to the end of the three-character elements:

    use strict; use warnings; while (<>) { s/\A...\K\Z/_/; print; }

    Usage: perl script.pl inFile [>outFile]

    The last, optional parameter directs output to a file.

    The substitution attempts to match an element that has only three characters, and the \K notation forces the substitution to Keep those three characters, and then adds the "_" to the end of those.

    Hope this helps!