Home > Archive > PERL Beginners > June 2005 > question about appending spaces to each line of a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
question about appending spaces to each line of a file
|
|
| Nupur Pande 2005-06-03, 8:55 pm |
| Hi,
I have a weird problem in perl.
I want to read each line from a file, chomp off the newline character,
append 6694 spaces to the end of each line and then output the line into
a new file.
The code I have is this:
while ($line = <Filehandle1> ) {
chomp $line;
$lengthofLine = length($line);
for ($i = 0; $i < 6694; $i++) {
$line = $line.' ';
}
print $lengthofLine." ".$line."\n";
print outfile1 "$lengthofLine"."$line"."\n";
} #endwhile
However, the output file looks very strange. The first line doesn't have
any appended spaces whatsoever and from the second line onwards, the
spaces are appended at the beginning of the line instead of the end.
Please help!!!
| |
| Jeff 'japhy' Pinyan 2005-06-03, 8:55 pm |
| On Jun 3, Nupur Pande said:
> I want to read each line from a file, chomp off the newline character,
> append 6694 spaces to the end of each line and then output the line into
> a new file.
>
> while ($line = <Filehandle1> ) {
> chomp $line;
> $lengthofLine = length($line);
Ok so far...
> for ($i = 0; $i < 6694; $i++) {
> $line = $line.' ';
> }
YECH! Didn't you think to yourself "there has to be a better way to write
this code?"
$line .= (" " x 6694);
That appends 6,694 spaces to the end of $line.
> print $lengthofLine." ".$line."\n";
>
> print outfile1 "$lengthofLine"."$line"."\n";
Those can be:
print "$lengthofLine $line\n";
print outfile1 "$lengthofLine$line\n";
> } #endwhile
> However, the output file looks very strange. The first line doesn't have
> any appended spaces whatsoever and from the second line onwards, the
> spaces are appended at the beginning of the line instead of the end.
That doesn't sound right... you *are* chomp()ing the string, so the
newline should disappear. Perhaps you also have \r (carriage return) at
the end of each line too?
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Chris Devers 2005-06-04, 3:57 am |
| On Fri, 3 Jun 2005, Nupur Pande wrote:
> I want to read each line from a file, chomp off the newline character,
> append 6694 spaces to the end of each line and then output the line
> into a new file.
>
> The code I have is this:
>
> [...]
>
> for ($i = 0; $i < 6694; $i++) {
> $line = $line.' ';
> }
Ouch.
$ perl -le '$i = "n" x 10; print $i'
nnnnnnnnnn
$
So why not just do something like
$line = ' ' x 6694;
?
That would reduce your whole sample to something like this (assuming
that you meant to 'use strict' and 'use warnings', which in turn assumes
that you meant to declare all your variables with 'my'):
my $space = ' ' x 6694; # no need for this to be in the loop!
while (my $line = <Filehandle1> ) {
chomp $line;
my $lengthofLine = length($line);
$line .= $space;
print "$lengthofLine $line\n";
print outfile1 "$lengthofLine$line\n";
} #endwhile
Or if I misunderstand you, and each line needs to be 6694 characters
wide, then something like this might help:
my $space = ' ' x 6694; # no need for this to be in the loop!
while (my $line = <Filehandle1> ) {
chomp $line;
my $lengthofLine = length($line);
my $padding = 6694 - $lengthofLine;
my $line .= $padding;
print "$lengthofLine $line\n";
print outfile1 "$lengthofLine$line\n";
} #endwhile
And the way you're concatenating your print statements also looks odd.
In general you can just put it all in double-quoted strings without
concatenating the parts together, as you're doing, and the code ends up
being much clearer as a result.
Helpful?
--
Chris Devers
| |
| Nupur Pande 2005-06-06, 3:56 pm |
| Hi All,
That was helpful! Thank you very much. The problem was that I was using
a DOS file and I think the chomp was probably able to remove one of the
newlines and not a \r. The interesting thing is "chomp"ing twice did not
seem to help either. However, 1 "chomp" and 1 "chop" did. Just thought
I'd share that with you all.
Thanks again!
Nupur
-----Original Message-----
From: Jeff 'japhy' Pinyan [mailto:japhy@perlmonk.org]=20
Sent: Friday, June 03, 2005 6:59 PM
To: Nupur Pande
Cc: beginners@perl.org
Subject: Re: question about appending spaces to each line of a file
On Jun 3, Nupur Pande said:
> I want to read each line from a file, chomp off the newline character,
> append 6694 spaces to the end of each line and then output the line
into
> a new file.
>=20
> while ($line =3D <Filehandle1> ) {
> chomp $line;
> $lengthofLine =3D length($line);
Ok so far...
> for ($i =3D 0; $i < 6694; $i++) {
> $line =3D $line.' ';
> }
YECH! Didn't you think to yourself "there has to be a better way to
write=20
this code?"
$line .=3D (" " x 6694);
That appends 6,694 spaces to the end of $line.
> print $lengthofLine." ".$line."\n";
>
> print outfile1 "$lengthofLine"."$line"."\n";
Those can be:
print "$lengthofLine $line\n";
print outfile1 "$lengthofLine$line\n";
> } #endwhile
> However, the output file looks very strange. The first line doesn't
have
> any appended spaces whatsoever and from the second line onwards, the
> spaces are appended at the beginning of the line instead of the end.
That doesn't sound right... you *are* chomp()ing the string, so the=20
newline should disappear. Perhaps you also have \r (carriage return) at
the end of each line too?
--=20
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Offer Kaye 2005-06-06, 3:56 pm |
| On 6/6/05, Nupur Pande wrote:
> The interesting thing is "chomp"ing twice did not
> seem to help either.=20
Of course not. "chomp" removes "$/" only, and unless you changed it,
it is equal (on Linux) to "\n". Read "perldoc -f chomp"
(http://perldoc.perl.org/functions/chomp.html). As an experiment, try
doing:
while (defined(my $line =3D <Filehandle1> )) {
local $/ =3D "\r\n";
chomp $line;
# rest of code...
}
HTH,
--=20
Offer Kaye
| |
| Chris Devers 2005-06-06, 3:56 pm |
| On Mon, 6 Jun 2005, Offer Kaye wrote:
> On 6/6/05, Nupur Pande wrote:
>
> Of course not. "chomp" removes "$/" only, and unless you changed it,
> it is equal (on Linux) to "\n".
Right. chomp() once or chomp() 100 times will have the same result.
Nupur may be thinking of chop(), which just removes the last character;
calling it more than once will keep truncating the string, no matter
whatthe trailing character is.
Note though that multiple chop() calls often won't be what you wanted,
and that you're better off updating $/ to get chomp() to behave than to
just use chop()chop() and hope for the best...
--
Chris Devers
|
|
|
|
|