FAQ
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

Search Discussions

  • Rob Coops at Aug 3, 2010 at 10:43 am

    On Tue, Aug 3, 2010 at 12:26 PM, Sharan Basappa 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@perl.org
    For additional commands, e-mail: beginners-help@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
  • Sharan Basappa at Aug 3, 2010 at 11:11 am
    Thanks a lot, Rob ...
    On Tue, Aug 3, 2010 at 4:13 PM, Rob Coops wrote:

    On Tue, Aug 3, 2010 at 12:26 PM, Sharan Basappa 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@perl.org
    For additional commands, e-mail: beginners-help@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 on cpan. Which is a much more industrial strength solution
    then reinventing the wheel is likely to be.
    Regards,
    Rob
  • Shawn H Corey at Aug 3, 2010 at 12:45 pm

    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", @myarray);
    or
    foreach my $value ( @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.
  • Chas. Owens at Aug 3, 2010 at 1:48 pm

    On Tue, Aug 3, 2010 at 08:44, Shawn H Corey wrote:
    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", @myarray);
    or
    foreach my $value ( @myarray ) {
    print $value . "\n";
    When printing, use a list; it's faster.

    print $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 = "foo";

    my %subs = (
    string => sub { print $bit_bucket "$s\n" },
    comma => sub { print $bit_bucket $s, "\n" },
    period => 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.
  • Chas. Owens at Aug 3, 2010 at 2:10 pm

    On Tue, Aug 3, 2010 at 09:47, Chas. Owens wrote:
    On Tue, Aug 3, 2010 at 08:44, Shawn H Corey wrote:
    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", @myarray);
    or
    foreach my $value ( @myarray ) {
    print $value . "\n";
    When printing, use a list; it's faster.

    print $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.
    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=Deparse -e 'print "foo" . "\n"'
    print "foo\n";

    perl -MO=Deparse -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 = "foo";

    my %subs = (
    string => sub { print $bit_bucket "$s\n" },
    comma => sub { print $bit_bucket $s, "\n" },
    period => 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 @a = 1 .. 10;

    my %subs = (
    string => 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 => 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 => 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.
  • C.DeRykus at Aug 3, 2010 at 9:40 pm

    On Aug 3, 7:10 am, chas.ow...@gmail.com ("Chas. Owens") wrote:
    On Tue, Aug 3, 2010 at 09:47, Chas. Owens wrote:
    On Tue, Aug 3, 2010 at 08:44, Shawn H Corey wrote:
    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", @myarray);
    or
    foreach my $value ( @myarray ) {
    print $value . "\n";
    When printing, use a list; it's faster.
    print $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.
    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=Deparse -e 'print "foo" . "\n"'
    print "foo\n";

    perl -MO=Deparse -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).
    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.  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 = "foo";

    my %subs = (
    string => sub { print $bit_bucket "$s\n"    },
    comma  => sub { print $bit_bucket $s, "\n"  },
    period => 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%     --
    ...
    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
  • Rob Dixon at Aug 4, 2010 at 12:59 am
    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
  • Philip Potter at Aug 4, 2010 at 12:30 pm

    On 4 August 2010 01:59, Rob Dixon 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedAug 3, '10 at 10:26a
activeAug 4, '10 at 12:30p
posts9
users7
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase