Output:use strict; my $string = 'one two three four five six seven eight nine ten eleven twelve +thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty' +; { local $, = ', '; print first_x( $string, 10 ), "\n"; } print scalar first_x( $string, 10 ), "\n"; sub first_x { return wantarray ? ( split /\s+/, $_[0], $_[1] + 1 )[ 0 .. $_[1] - 1 ] : join ' ', ( split /\s+/, $_[0], $_[1] + 1 )[ 0 .. $_[1] - 1 +]; }
If called in list context, it returns a list with the "x" first words. If called in scalar context it returns a string of the first "x" words joined by spaces.one, two, three, four, five, six, seven, eight, nine, ten, one two three four five six seven eight nine ten
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
In reply to Re: Truncate Data from MySQL
by CountZero
in thread Truncate Data from MySQL
by rich731
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |