wrap a really long line

K

Ken Sington

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.
 
G

gnari

Ken Sington said:
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
 
A

Anno Siegel

Ken Sington said:
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
 
B

Ben Morrow

Quoth "gnari said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top