Suresh Govindachar wrote:
> Gunnar Hjalmarsson wrote:
>> Suresh Govindachar wrote:
>>> What is a good way (with respect to speed)
>>> to un-chomp an array?
>>>
>>> for(my $i=0; $i<scalar @lines; $i++)
>>> {
>>> $lines[$i] .= "\n";
>>> }
>>
>> This is simpler code:
>>
>> print map "$_\n", @lines;
>
> Doesn't the above code print? I actually want to
> change the array @lines.
Then do:
$_ .= "\n" for @lines;
Besides, according to Scott's benchmark, that's fastest as well.
However, I can't help to ask: Was it motivated to chomp the lines in
the first place? Did you for instance do something like this:
my @lines = <FILE>;
for (@lines) {
chomp;
...
??
In that case, maybe it would have been possible to keep the trailing
newline characters all through instead?
And another question: Was it motivated to load all the lines into
memory, or would it have been sufficient to process them one at a time:
while (<FILE>) {
...
--
Gunnar Hjalmarsson
Email:
http://www.gunnar.cc/cgi-bin/contact.pl