add newline

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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sharan Basappa [ Di, 03 August 2010 12:26 ] [ ID #2045491 ]

Re: add newline

--001485f2c6c0db8b25048ce8fd23
Content-Type: text/plain; charset=UTF-8

On Tue, Aug 3, 2010 at 12:26 PM, Sharan Basappa <sharan.basappa [at] 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-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] 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 ( [at] myarray, $value . "\n");

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

Third you could of course when you are printing the values from the array
add the linefeeds:
print join("\n", [at] myarray);
or
foreach my $value ( [at] 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

--001485f2c6c0db8b25048ce8fd23--
Rob Coops [ Di, 03 August 2010 12:43 ] [ ID #2045492 ]

Re: add newline

Thanks a lot, Rob ...

On Tue, Aug 3, 2010 at 4:13 PM, Rob Coops <rcoops [at] gmail.com> wrote:
>
>
> On Tue, Aug 3, 2010 at 12:26 PM, Sharan Basappa <sharan.basappa [at] 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-unsubscribe [at] perl.org
>> For additional commands, e-mail: beginners-help [at] 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:
> =A0push ( [at] myarray, $value . "\n");
> Second you could of course add the line feeds as seperate array values:
> =A0push ( [at] myarray, $value);
> =A0push ( [at] myarray, "\n");
> Third you could of course when you are printing the values from the array
> add the linefeeds:
> =A0print join("\n", [at] myarray);
> or
> =A0foreach my $value ( [at] myarray ) {
> =A0=A0print $value . "\n";
> =A0or
> =A0=A0print $value;
> =A0=A0print "\n";
> =A0}
> 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 a=
t
> 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 on cpan. Which is a much more industrial strength soluti=
on
> then reinventing the wheel is likely to be.
> Regards,
> Rob

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sharan Basappa [ Di, 03 August 2010 13:11 ] [ ID #2045493 ]

Re: add newline

On 10-08-03 06:43 AM, Rob Coops wrote:
> Third you could of course when you are printing the values from the array
> add the linefeeds:
> print join("\n", [at] myarray);
> or
> foreach my $value ( [at] myarray ) {
> print $value . "\n";

When printing, use a list; it's faster.

print $value, "\n";


--
Just my 0.00000002 million dollars worth,
Shawn

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Di, 03 August 2010 14:44 ] [ ID #2045494 ]

Re: add newline

On Tue, Aug 3, 2010 at 08:44, Shawn H Corey <shawnhcorey [at] gmail.com> wrote:
> On 10-08-03 06:43 AM, Rob Coops wrote:
>>
>> Third you could of course when you are printing the values from the arra=
y
>> add the linefeeds:
>> =C2=A0print join("\n", [at] myarray);
>> or
>> =C2=A0foreach my $value ( [at] myarray ) {
>> =C2=A0 print $value . "\n";
>
> When printing, use a list; it's faster.
>
> =C2=A0 =C2=A0print $value, "\n";
snip

I hate it when some makes a blanket statement of "it's faster" without
providing a benchmark or a reason. In the simple case, the comma is
slower than the period and interpolation is slower still.

One item:
#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

open my $bit_bucket, ">", "/dev/null"
or die "$!";

my $s =3D "foo";

my %subs =3D (
string =3D> sub { print $bit_bucket "$s\n" },
comma =3D> sub { print $bit_bucket $s, "\n" },
period =3D> sub { print $bit_bucket $s . "\n" },
);

for my $sub (keys %subs) {
print "$sub: ", $subs{$sub}(), "\n";
}

Benchmark::cmpthese -1, \%subs;

Ten Items:


--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Di, 03 August 2010 15:47 ] [ ID #2045495 ]

Re: add newline

On Tue, Aug 3, 2010 at 09:47, Chas. Owens <chas.owens [at] gmail.com> wrote:
> On Tue, Aug 3, 2010 at 08:44, Shawn H Corey <shawnhcorey [at] gmail.com> wrote=
:
>> On 10-08-03 06:43 AM, Rob Coops wrote:
>>>
>>> Third you could of course when you are printing the values from the arr=
ay
>>> add the linefeeds:
>>> =C2=A0print join("\n", [at] myarray);
>>> or
>>> =C2=A0foreach my $value ( [at] myarray ) {
>>> =C2=A0 print $value . "\n";
>>
>> When printing, use a list; it's faster.
>>
>> =C2=A0 =C2=A0print $value, "\n";
> snip
>
> I hate it when some makes a blanket statement of "it's faster" without
> providing a benchmark or a reason. =C2=A0In the simple case, the comma is
> slower than the period and interpolation is slower still.

Whoops, accidentally sent too early. My point was that even though
concatenation is faster than pass a list or an interpolated string
(contrary to the claim made earlier), it isn't really that much
faster. Especially once you factor in the cost of writing to disk
(which the benchmark avoids to prevent noise). Now, you might make
the argument that the comma is clearer than the concatenation, but
that point is debatable. For one thing, these two statements are not
identical:

print "foo", "\n";
print "foo" . "\n";

The first will be affected by the value of $, and the second won't.
The second will also benefit from compile time constant folding:

perl -MO=3DDeparse -e 'print "foo" . "\n"'
print "foo\n";

perl -MO=3DDeparse -e 'print "foo", "\n"'
print 'foo', "\n";

Personally, I use string interpolation even though it is the slowest
of the three methods. I find string interpolation to be superior to
passing a list because it is not affected by $, and more readable than
having tiny periods floating around. It also requires fewer
characters on average (you usually have at least one set of double
quotes in the expression already).

To sum up: if you are looking for something like this to speed up your
program than you are well and truly hosed. Choose a method that looks
right to your team and has the right behavior. Profile your code to
find spots were you are slow and optimize those spots by changing the
algorithm or moving code down a level to C (via XS or Inline::C), not
by relying on folk-wisdom micro-optimizations.

One item:
Rate string comma period
string 3588672/s -- -3% -11%
comma 3714590/s 4% -- -7%
period 4014817/s 12% 8% --

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

open my $bit_bucket, ">", "/dev/null"
or die "$!";

my $s =3D "foo";

my %subs =3D (
string =3D> sub { print $bit_bucket "$s\n" },
comma =3D> sub { print $bit_bucket $s, "\n" },
period =3D> sub { print $bit_bucket $s . "\n" },
);

for my $sub (keys %subs) {
print "$sub: ", $subs{$sub}(), "\n";
}

Benchmark::cmpthese -1, \%subs;

Ten items:
Rate string comma period
string 1052183/s -- -1% -2%
comma 1061926/s 1% -- -1%
period 1071850/s 2% 1% --

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

open my $bit_bucket, ">", "/dev/null"
or die "$!";

my [at] a =3D 1 .. 10;

my %subs =3D (
string =3D> sub { print $bit_bucket
"$a[0]$a[1]$a[1]$a[2]$a[3]$a[4]$a[5]$a[6]$a[7]$a[8]$a[9]\n" },
comma =3D> sub { print $bit_bucket
$a[0],$a[1],$a[1],$a[2],$a[3],$a[4],$a[5],$a[6],$a[7],$a[8], $a[9],
"\n" },
period =3D> sub { print $bit_bucket
$a[0].$a[1].$a[1].$a[2].$a[3].$a[4].$a[5].$a[6].$a[7].$a[8]. $a[9].
"\n" },
);

for my $sub (keys %subs) {
print "$sub: ", $subs{$sub}(), "\n";
}

Benchmark::cmpthese -1, \%subs;

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Di, 03 August 2010 16:10 ] [ ID #2045496 ]

Re: add newline

On Aug 3, 7:10=A0am, chas.ow... [at] gmail.com ("Chas. Owens") wrote:
> On Tue, Aug 3, 2010 at 09:47, Chas. Owens <chas.ow... [at] gmail.com> wrote:
> > On Tue, Aug 3, 2010 at 08:44, Shawn H Corey <shawnhco... [at] gmail.com> wro=
te:
> >> On 10-08-03 06:43 AM, Rob Coops wrote:
>
> >>> Third you could of course when you are printing the values from the a=
rray
> >>> add the linefeeds:
> >>> =A0print join("\n", [at] myarray);
> >>> or
> >>> =A0foreach my $value ( [at] myarray ) {
> >>> =A0 print $value . "\n";
>
> >> When printing, use a list; it's faster.
>
> >> =A0 =A0print $value, "\n";
> > snip
>
> > I hate it when some makes a blanket statement of "it's faster" without
> > providing a benchmark or a reason. =A0In the simple case, the comma is
> > slower than the period and interpolation is slower still.
>
> Whoops, accidentally sent too early. =A0My point was that even though
> concatenation is faster than pass a list or an interpolated string
> (contrary to the claim made earlier), it isn't really that much
> faster. =A0Especially once you factor in the cost of writing to disk
> (which the benchmark avoids to prevent noise). =A0Now, you might make
> the argument that the comma is clearer than the concatenation, but
> that point is debatable. =A0For one thing, these two statements are not
> identical:
>
> print "foo", "\n";
> print "foo" . "\n";
>
> The first will be affected by the value of $, and the second won't.
> The second will also benefit from compile time constant folding:
>
> perl -MO=3DDeparse -e 'print "foo" . "\n"'
> print "foo\n";
>
> perl -MO=3DDeparse -e 'print "foo", "\n"'
> print 'foo', "\n";
>
> Personally, I use string interpolation even though it is the slowest
> of the three methods. =A0I find string interpolation to be superior to
> passing a list because it is not affected by $, and more readable than
> having tiny periods floating around. =A0It also requires fewer
> characters on average (you usually have at least one set of double
> quotes in the expression already).

I agree totally.

>
> To sum up: if you are looking for something like this to speed up your
> program than you are well and truly hosed. =A0Choose a method that looks
> right to your team and has the right behavior. =A0Profile your code to
> find spots were you are slow and optimize those spots by changing the
> algorithm or moving code down a level to C (via XS or Inline::C), not
> by relying on folk-wisdom micro-optimizations.
>
> One item:
> =A0 =A0 =A0 =A0 =A0 =A0 Rate string =A0comma period
> string 3588672/s =A0 =A0 -- =A0 =A0-3% =A0 -11%
> comma =A03714590/s =A0 =A0 4% =A0 =A0 -- =A0 =A0-7%
> period 4014817/s =A0 =A012% =A0 =A0 8% =A0 =A0 --
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use Benchmark;
>
> open my $bit_bucket, ">", "/dev/null"
> =A0 =A0 =A0 =A0or die "$!";
>
> my $s =3D "foo";
>
> my %subs =3D (
> =A0 =A0 =A0 =A0string =3D> sub { print $bit_bucket "$s\n" =A0 =A0},
> =A0 =A0 =A0 =A0comma =A0=3D> sub { print $bit_bucket $s, "\n" =A0},
> =A0 =A0 =A0 =A0period =3D> sub { print $bit_bucket $s . "\n" },
> );
>
> for my $sub (keys %subs) {
> =A0 =A0 =A0 =A0print "$sub: ", $subs{$sub}(), "\n";
>
> }
>
> Benchmark::cmpthese -1, \%subs;
>
> Ten items:
> =A0 =A0 =A0 =A0 =A0 =A0 Rate string =A0comma period
> string 1052183/s =A0 =A0 -- =A0 =A0-1% =A0 =A0-2%
> comma =A01061926/s =A0 =A0 1% =A0 =A0 -- =A0 =A0-1%
> period 1071850/s =A0 =A0 2% =A0 =A0 1% =A0 =A0 --
> ...

There was a recent thread about the slowness of
concatenation on Win32 (can't remember, maybe
a perl issue though).

Adding a couple of small tweaks for Win32 and
re-running seems to confirm:

use File::Spec qw(devnull);

open my $bit_bucket, ">", File::Spec->devnull()
or die "$!";
...

Result:

Rate period string comma
period 453142/s -- -4% -6%
string 469903/s 4% -- -2%
comma 481771/s 6% 3% --

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Di, 03 August 2010 23:40 ] [ ID #2045497 ]

Re: add newline

Speed of execution is the last goal of all.

First of all make your program functional and intelligible.

Only after that, if you have problems with resources (including time,
disk space, or processor) tune it to be more efficient.

HTH,

- Rob

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Mi, 04 August 2010 02:59 ] [ ID #2045541 ]

Re: add newline

On 4 August 2010 01:59, Rob Dixon <rob.dixon [at] gmx.com> wrote:
>
> Speed of execution is the last goal of all.
>
> First of all make your program functional and intelligible.
>
> Only after that, if you have problems with resources (including time, disk
> space, or processor) tune it to be more efficient.

This, a thousand times. It's easier to make a correct and readble
program fast than it is to make a fast program correct and readable.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Philip Potter [ Mi, 04 August 2010 14:30 ] [ ID #2045542 ]
Perl » gmane.comp.lang.perl.beginners » add newline

Vorheriges Thema: Win32::Ole and Powerpoint
Nächstes Thema: printing grep results in Perl