Chris Dolan wrote:
>
> A. Pagaltzis wrote:
> > * Dave Arnold <[EMAIL PROTECTED]> [2002-11-21 02:45]:
> >
> >>$str = join ', ', @names;
> >>if ( length($str) > 90 )
> >>{
> >> substr($str,rindex($str,",",90)) = ", etc.";
> >>}
> >
> > Nice, and so obvious too - in retrospect, of course.
> >
> > The only thing I don't like about it is it still joins
> > everything, even if it only needs 3 out of 10,000 elements.
>
> How about a simple change of the first line to:
>
> $str = join ', ', @names[0..30];
>
> With a "," and a " ", each element must have at least 3 characters
> (ignoring the posibility of empty elements), so there will be at most 30
> entries in the final string. I include the 31st to trigger the
> ", etc.".
If you use a 31 element slice on a array with only 10 elements you will get
21 undef values on the end.
$ perl -le'@array = qw/a b c d e f/; $str = join ", ", @array[0..30]; print $str'
a, b, c, d, e, f, , , , , , , , , , , , , , , , , , , , , , , , ,
You have to use splice() to avoid this.
$ perl -le'@array = qw/a b c d e f/; $str = join ", ", splice @array, 0, 30; print
$str'
a, b, c, d, e, f
John
--
use Perl;
program
fulfillment