in reply to Re: Truncate Data from MySQL
in thread Truncate Data from MySQL

Going off on a bit of a tangent, it just occurred to me that it should be possible to write the first_x function recursively, to reduce code duplication. Ie:
sub first_x { return wantarray ? ( split /\s+/, $_[0], $_[1] + 1 )[ 0 .. $_[1] - 1 ] : join ' ', first_x( @_ ); }
Maybe a similar pattern could be applied in other circumstances as well.

--
use JAPH;
print JAPH::asString();

Replies are listed 'Best First'.
Re^3: Truncate Data from MySQL
by spazm (Monk) on Jul 08, 2009 at 20:01 UTC
    I'm not particularly fond of the extra trip through the sub when we could use a lexical to store the data:
    sub first_x { my @data = ( split /\s+/, $_[0], $_[1] + 1 )[ 0 .. $_[1] - 1 ]; return wantarray ? @data : join ' ' , @data ; }
    then again, I don't like the 0..n slice. Why not just use pop?
    sub first_x { my @data = split /\s+/, $_[0], $_[1] + 1 ; pop(@data); #throw away last element return wantarray ? @data : join ' ' , @data ; }
    peace, spazm