wrap a really long line

Discussion in 'Perl Misc' started by Ken Sington, Jun 14, 2004.

  1. Ken Sington

    Ken Sington Guest

    criticism is good.
    here's my function to wrap really long lines:
    very basic.

    # wrap long lines ###################################
    sub wrapLine {
    my ($line) = @_;
    $line =~ s/.{70}/$&\n/g;
    return "$line"; #(yeah, I know, last value is returned automatically)
    }

    using standards:
    #!/usr/bin/perl
    use strict;
    use warnings;
    of course.

    PROG 1
    my $longline="blah, " x 300;
    print &wrapLine($longline);


    --- OR ---

    PROG 2
    open READ, "twoMB_bigFile.txt;
    while (<READ>){
    print &wrapLine($_);
    }
    close READ;


    --- OR ---

    PROG 3
    open READ, "twoMB_bigFile.txt";
    my @big = <READ>;
    close READ;

    foreach (@big) {
    print &wrapLine($_);
    }



    --- OR ---

    PROG 4
    open READ, "twoMB_bigFile.txt";
    my @big = <READ>;
    close READ;

    while (@big) {
    print &wrapLine($_);
    }




    Anyone think there's a better way to wrap a long line?


    I notice that in PROG.4, with the while loop, the script seems to just
    hang. however, the foreach in PROG 3 seems to run just fine.
    I was told by some "expert" that while handles large input better than
    foreach.

    PROG 1 and PROG 2 work as expected.
     
    Ken Sington, Jun 14, 2004
    #1
    1. Advertising

  2. Ken Sington

    gnari Guest

    "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    news:...
    > criticism is good.
    > here's my function to wrap really long lines:
    > very basic.
    >
    > # wrap long lines ###################################
    > sub wrapLine {
    > my ($line) = @_;
    > $line =~ s/.{70}/$&\n/g;


    the use of $& degrades performance, if that is an issue.

    > return "$line"; #(yeah, I know, last value is returned

    automatically)

    a) yes, but the last value is not what you think!
    b) unnecessary use of quotes. better is:
    returm $line;

    >
    > PROG 1
    > my $longline="blah, " x 300;
    > print &wrapLine($longline);


    the '&' not needed

    > PROG 2
    > open READ, "twoMB_bigFile.txt;


    syntax error, missing failure check

    >
    > while (@big) {
    > print &wrapLine($_);
    > }


    you want foreach (@big) here, not while, because
    a) the test (@big) never changes from true to false
    b) $_ is not set to anything useful

    there is more, but start with these.

    gnari
     
    gnari, Jun 14, 2004
    #2
    1. Advertising

  3. Ken Sington

    Anno Siegel Guest

    Ken Sington <ken_sington@nospam_abcdefg.com> wrote in comp.lang.perl.misc:
    > criticism is good.
    > here's my function to wrap really long lines:
    > very basic.
    >
    > # wrap long lines ###################################
    > sub wrapLine {
    > my ($line) = @_;
    > $line =~ s/.{70}/$&\n/g;
    > return "$line"; #(yeah, I know, last value is returned automatically)
    > }



    sub wrapline { join "\n", shift =~ /(.{1,70})/g }

    Anno
     
    Anno Siegel, Jun 14, 2004
    #3
  4. Ken Sington

    Ben Morrow Guest

    Quoth "gnari" <>:
    > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > news:...
    > > criticism is good.
    > > here's my function to wrap really long lines:
    > > very basic.
    > >
    > > # wrap long lines ###################################
    > > sub wrapLine {
    > > my ($line) = @_;
    > > $line =~ s/.{70}/$&\n/g;

    >
    > the use of $& degrades performance, if that is an issue.


    Note that the right answer (s/(.{70})/$1\n/g) will not increase the
    speed of *that* regex, it will just prevent a speed penalty on matches
    which don't use capturing brackets.

    To the OP: try Text::Wrap or Text::Format, or even the faq...

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Jun 14, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    746
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,244
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    736
    Chronic Philharmonic
    May 10, 2008
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    267
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    274
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page