Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Counting characters without a space

by sundialsvc4 (Abbot)
on Jul 23, 2018 at 14:28 UTC ( #1219106=note: print w/replies, xml ) Need Help??


in reply to Re^2: Counting characters without a space
in thread Counting characters without a space

Yup, no matter what you’re doing, “look around ... CPAN has already done it!”

Having said that, the algorithm that first occurred to me consists of splitting the string into an array, then building up the output string one word at a time, spitting out each partial string as you go.   (This example has many warts in it.)

use strict; use warnings; my $str = "This is a very long string more than thirty characters wide."; my @split = split(/\s+/, $str); my $output = ''; foreach my $word (@split) { $output .= "$word "; if (length($output) > 30) { print "$output\n"; $output = ''; } } print "$output\n"; This is a very long string more than thirty characters wide.

Replies are listed 'Best First'.
Re^4: Counting characters without a space
by marto (Archbishop) on Jul 23, 2018 at 14:33 UTC

    "Yup, no matter what you’re doing, “look around ... CPAN has already done it!”"

    Except when you say otherwise, right? One of several examples: Re: use Memoize;.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1219106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2019-12-07 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (162 votes). Check out past polls.

    Notices?