Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > wrap a really long line

Reply
Thread Tools

wrap a really long line

 
 
Ken Sington
Guest
Posts: n/a
 
      06-14-2004
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.
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      06-14-2004
"Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
news:KXOdnbq1X6-1aVHdRWPC-...
> 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




 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-14-2004
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
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-14-2004

Quoth "gnari" <>:
> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:KXOdnbq1X6-1aVHdRWPC-...
> > 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'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
To wrap or not to wrap? Aaron Fude Java 12 05-10-2008 06:33 PM
Wrap computer components in bubble wrap? Ickshka Computer Support 7 05-05-2006 05:54 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 1 09-25-2003 06:15 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 0 09-22-2003 02:36 PM



Advertisments