Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

add a line based on maxline length

by softworkz (Monk)
on Jul 31, 2002 at 19:38 UTC ( [id://186573]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks!
I wrote this script and I need some suggestions.
The purpose of it is to read in each line to determine which line is the "maxline" length and take that length and add maxline length spaces and a \n at the end of the file

1. Is there a better way to do this? (shorter version maybe?)
2. Does anyone see anything wrong with the code?
3. The files are supposed to be text files run on windows..

#!/usr/bin/perl -w use strict; my $line = <DATA>; chomp($line); print "$line\n"; my $vc=0; #0 for const and 1 for var my $maxline = length($line); $_ = $line; if (m/.*\r$/){ #carriage return $maxline = $maxline - 1; } if (m/.*\032$/){ #ctrl-Z $maxline = $maxline - 1; } my $originalmaxline = $maxline; while(<DATA>) { chomp; my $newline = $_; print "$newline\n"; my $newmaxline = length($newline); if (m/.*\r$/) { #carriage return $maxline = $maxline - 1; } if (m/.*\032$/) {#ctrl-Z $maxline = $maxline - 1; } if ($newmaxline != $originalmaxline) { $vc = 1; } if ($newmaxline > $maxline) { $maxline = $newmaxline; } } #----------------------------------------------- # using the char X to see it print a space based on maxline count # in this case it's 29 #----------------------------------------------- for (my $i=0;$i<$maxline;$i++) { my $padding = "X"; print $padding; } print "\n"; __DATA__ This is my file it doesn't have a blank line at the end of it based on the maxline count of the file that it read.


thankz!

Replies are listed 'Best First'.
Re: add a line based on maxline length
by mkmcconn (Chaplain) on Jul 31, 2002 at 19:45 UTC
    #!perl -w use strict; my $padding = 0; for (<DATA>){ chomp; $padding = $padding > length($_) ? $padding : length($_); } print "X" x $padding, "\n"; __DATA__ This is my file it doesn't have a blank line at the end of it based on the maxline count of the file that it read.

    update Wrong idea, eh? How about this?
    #!perl -w use strict; my $padding = 0; for (<DATA>){ chomp; $padding = $padding > length($_) ? $padding : length($_); print "$_\n"; } print " " x $padding, "X\n"; __DATA__ This is my file it doesn't have a blank line at the end of it based on the maxline count of the file that it read.

    mkmcconn
      Surely a Perl script of that brevity can be reduced to a one-liner:

      perl -lpe '$p = length if $p < length; END {print "X" x $p}'

      --Dave

Re: add a line based on maxline length
by Popcorn Dave (Abbot) on Jul 31, 2002 at 20:05 UTC
    Well I was able to cut it down to the following. I don't know that it's any faster or not but it's less to look at. :)

    #!/usr/bin/perl -w use strict; my $line; my $maxline = 0; # make sure you get the length of the first line while($line = <DATA>) { chomp($line); print "$line\n"; $maxline = length($line) if length($line)>$maxline; $maxline -= 1 if $line =~ m/.*\032$/; } #----------------------------------------------- # using the char X to see it print a space based on maxline count # in this case it's 29 #----------------------------------------------- print "X" for (1..$maxline); print "\n"; __DATA__ This is my file it doesn't have a blank line at the end of it based on the maxline count of the file that it read. <P>

    A couple of things I did notice though:

    1. If you're chomping the line coming in, why are you checking it for \r again? Maybe I'm overlooking something there and if so, I know someone will point it out.

    2. Why are you checking the first line of data outside your loop?

    3. What is all the ( what appears to be - to me anyway ) extraneous code at the top or is that another part of your program that you didn't post?

    Hope that helps!

    Some people fall from grace. I prefer a running start...

      Thankz! a bunch!! All of you should me flaws with my code! and all of you were correct with what I needed. Shendal, I had to add this
      $max = $max-1; # I don't want it to count the \r|\032 print "X" x $max,"\n";
      A++ help! thankz!

      Now on to what I was thinking when I wrote it... tomhukins $vc is a left over for "variable constant" a whole other issue..
      Good idea though if I have to write the same code re-think things..

      and... redsquirrel?.. I was looking for a "shorter" version, not the "shortest" version! Thatz sick! =:-)
Re: add a line based on maxline length
by Shendal (Hermit) on Jul 31, 2002 at 19:57 UTC
    Is this what you're looking for?
    use strict; my $max = 0; foreach (<DATA>) { s/(\r|\032)$//; $max = length($_) if length($_) > $max; } print " " x $max; __DATA__ This is my file it doesn't have a blank line at the end of it based on the maxline count of the file that it read.

    Cheers,
    Shendal
Re: add a line based on maxline length
by tomhukins (Curate) on Jul 31, 2002 at 19:57 UTC

    What is the reason for doing my $line = <DATA> followed by the processing code then calling the same processing code within the while (<DATA>) block? If you find yourself writing the same code in your program twice or more, you should place that code in a subroutine. In this case, however, you can process each line within the while block, so take out the code that reads and processes $line being sure to keep the declaration of $maxline outside this block.

    What is the purpose of $vc? You don't seem to use it for anything meaningful.

    Also, replace the for loop at the end with something like print " " x $maxlength (see perlop for details).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186573]
Approved by tomhukins
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-20 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found