in reply to Re: Re: reading file into an array
in thread reading file into an array
The elements of @lines has newlines. You either need to chomp them:
chomp(my @lines = <FILE>)In which case you print them with:
print FILE for @lines; # or, to be more verbose foreach my $line (@lines) { print FILE $line }
Or, you can leave off the chomp, and use the loop:
print FILE "$_\n" for @lines; # or, to be more verbose foreach my $line (@lines) { print FILE "$line\n"; }
Either you've removed the newlines or you haven't, your foreach loop has to match.
Of course, if this is all you're doing with @lines it'd be much simpler, and more kind to memory, to just read and write at the same time:
print OUTPUT_FILE while <INPUT_FILE>; # or, to be more verbose while ($line = <INPUT_FILE>) { # manipulation of $line goes here. print OUTPUT_FILE $line; }
perldoc -q 'entire file' covers this topic, 5.6.1's perldoc -q 'line in a file' helps to explain dealing with files, and 5.8.4's perldoc -q 'line in a file' tells you to use Tie::File.
|
|---|