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!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.