Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Display shortened paragraph

by jbrugger (Parson)
on Feb 01, 2006 at 05:58 UTC ( #526973=note: print w/replies, xml ) Need Help??


in reply to Display shortened paragraph

TIMTOWTDI
update: More or less the same idea as duff i see :)
#!/usr/bin/perl -w use strict; my $txt = "just wondering how i can have perl display part of my long +memo. basically i want the first lets say 255 charachters of the para +graph. im really new to perl so i don't know how i would come about t +his? a regex perhaps? just started reading about that today. so basic +ally something like"; my $n = 20; $txt =~ m/(.{$n})/gs; print $1;


ok, yet another way then :)
#!/usr/bin/perl -w use strict; my $txt = "just wondering how i can have perl display part of my long +memo. basically i want the first lets say 255 charachters of the para +graph. im really new to perl so i don't know how i would come about t +his? a regex perhaps? just started reading about that today. so basic +ally something like"; my @a = split("",$txt); for (my $i=0; $i<20; $i++) { print $a[$i]; }


"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Replies are listed 'Best First'.
Re^2: Display shortened paragraph
by Anonymous Monk on Feb 01, 2006 at 06:09 UTC
    hi! works, but how would i shorten the paragraph so that it does not cut off a word if it reaches the max char. limit? for example..."just wondering how..." compared to "just wonder h..." ---notice how it cuts off the "ow" in "how"....thanks
      Since people have shown how the use of a regex approach tends to be slower, here's a way to observe word boundaries (well, spaces between words, anyway) without using a regex:
      my $maxlen = 20; my $longtext = "This is some very long string that needs to be truncat +ed to $maxlen characters..."; my $trunctext = substr( $longtext, 0, rindex( $longtext, " ", $maxlen +)); print "$longtext\n$trunctext\n";
      The rindex function, like substr, is faster than a regex match.
      By using duff's solution with the Matching the word boundary \b so that you don't chop off the text in the middle of a word.

      update:
      A clumsy other way to do this :)
      use strict; use warnings; my $txt = "just wondering how i can have perl display part of my long +memo. basically i want the first lets say 255 charachters of the para +graph. im really new to perl so i don't know how i would come about t +his? a regex perhaps? just started reading about that today. so basic +ally something like"; my @a = split("",$txt); my $l = 7; for (my $i=0; $i < $l; $i++) { print $a[$i]; $l++ if ($i ==($l-1) && ($a[$i] ne " " )) ; }


      "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
        i meant by code given by duff, not graff :)
      actually the code given by graff works just fine:

      my $max = 230; (my $copy = $string) =~ s/(.{1,$max})\b.*/$1.../; print "$copy\n";
      Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://526973]
help
Chatterbox?
[ambrus]: nysus: make sure you enter at least two lines for the title and then preview, and read warnings the preview form prints
[davies]: There is an option (I have it set) to force preview before submission. Perhaps your option has been set (accidentally?) and you are not expecting it.
[ambrus]: sorry, I mean at least two words for the title and then preview
[ambrus]: davies: that option is the default. and it's not really "force", it just hides the button.
[marto]: yes, not reading the errors displayed has been a cause of this type of report in the past
[nysus]: Ambrus, ah. I think that was the problem. I'll try. Thanks!
[davies]: Ambrus: you've missed it by a couple of weeks, but consider next year's London Perl Workshop.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2017-12-15 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?