Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: substr question

by toolic (Bishop)
on Jun 18, 2010 at 16:52 UTC ( #845410=note: print w/replies, xml ) Need Help??

in reply to substr question

Something like this, where a "word" is defined by surrounding whitespace (\s):
use strict; use warnings; my $s = "Hello, I am a perl/mysql programmer, but am self taught so I +do not know all the awesome features Perl has, however, I am ok at it + though, I guess."; my $s2; for (split /\s+/, $s) { $s2 .= "$_ "; last if length($s2) > 100; } print $s2; __END__ Hello, I am a perl/mysql programmer, but am self taught so I do not kn +ow all the awesome features Perl

Replies are listed 'Best First'.
Re^2: substr question
by jwkrahn (Monsignor) on Jun 18, 2010 at 18:45 UTC

    Probably better as:

    my $s2; for ( split /(\s+)/, $s ) { last if length( $s2 . $_ ) > 100; $s2 .= $_; } print $s2;
      In fact, your code is worse for 2 reasons:
      • The output string $s2 no longer has any whitespace between words: Hello,Iamaperl/mysql...
      • Generates a warning: Use of uninitialized value $s2...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://845410]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2018-06-20 14:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.