Gossamer Forum
Home : General : Perl Programming :

Carriage return problems

Quote Reply
Carriage return problems
I'm writing a code that opens a file, reads its contents and puts every line (with modifications) in an array. Then I send this array to a new file. The only problem is that every line, except for the first line, in the new file has a single space at its beginning. I don't think I'm using my carriage return right. For example, if the old file read

Apples

Oranges

The new file would read

Apples

_Oranges

Where the "_" is not literally there but represents a single space (since I can't get the damn preview to look right)Cool.

Code:


open(WORDLIST, "file.txt");
while ($arrayword = <WORDLIST>) {

$arrayword = substr($arrayword, 0, 77)."\n";


I'm pretty sure this is the part of the code where the problem lies.

Any suggestions would help.

Thanks!!
Quote Reply
Re: [slappy] Carriage return problems In reply to
Try something like this. It will clean up newlines, leading and trailing spaces.

Code:
open(WORDLIST, "file.txt");
while ($arrayword = <WORDLIST>) {

$arrayword =~ s/^ //; # Remove leading space
$arrayword =~ s/ $//; # Remove trailing space
chomp $arrayword; # get rid of newlines...

$arrayword = substr($arrayword, 0, 77)."\n";

}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [slappy] Carriage return problems In reply to
If there are no leading spaces in the original file then there shouldn't be any in the newly printed file.

Something like this will work fine...

Code:
open OLD, "the old file" or die $!;
open NEW, ">the new file" or die $!;
while (<OLD>) {
chomp;
print NEW substr($_, 0, 77) . "\n";
}
close NEW;
close OLD;