On Tue, Aug 3, 2010 at 12:26 PM, Sharan Basappa <sharan.basa...@gmail.com>wrote:

> In my program, I am building a text file f that also contains newlines
> irst into an array.
> I push every line to the array, but how do I add new lines to this text?
>
> Regards,
> Sharan
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>
Hi Sharan,

There are several options (it is perl after all :-)

First you could when you are adding a value to the array also add the
newline character:
 push (@myarray, $value . "\n");

Second you could of course add the line feeds as seperate array values:
 push (@myarray, $value);
 push (@myarray, "\n");

Third you could of course when you are printing the values from the array
add the linefeeds:
 print join("\n", @myarray);
or
 foreach my $value ( @myarray ) {
  print $value . "\n";
 or
  print $value;
  print "\n";
 }

There are a few more options but these are the most common once you will see
around the perl world. It really depends on what you want to do with you
array. If you only use the array to print the output I would advise using
the join option as that is certainly the most efficient memory wise and at
least feeling wise the fastest way of working (though I have not verified
this) the second option is just silly and likely quite slow and memory
inefficient.

If you are using the array as a form of log file you might want to have a
look at 
log4perl<http://search.cpan.org/~mschilli/Log-Log4perl-1.29/lib/Log/Log4perl.pm>on
cpan. Which is a much more industrial strength solution then
reinventing
the wheel is likely to be.

Regards,

Rob

Reply via email to