[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] Finding the length of the longest string



> From: Andy Lester [mailto:andy@petdance.com]
> Sent: Wednesday, June 23, 1999 17:53
> To: Larry Rosler
> Subject: Re: [FWP] Finding the length of the longest string
> 
> >my $len = "";
> >$len ^= $_ for @data;
> >$len = length $len;
> >
> >Any better ideas?
> 
> How about map?
> 
> my $max = 0;
> map { ($len=length) > $max and $max = $len } @data;

This excerpt from perlfaq6 - Regexes 
...

What's wrong with using grep or map in a void context?
 
Both grep and map build a return list, regardless of their context.
This means you're making Perl go to the trouble of building up a
return list that you then just ignore.  That's no way to treat a
programming language, you insensitive scoundrel!



The Perl gods will get you for that!

BTW, note that in Andy's formulation, saving the value of length() and
reusing it is slower than calculating it twice.  length() ne C's library
function strlen().  length() is a Perl primitive, like addition or
assignment.  Old habits die hard!

-- 
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org