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

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



This would probably be MUCH slower... but it's kind of compact and fun.

(sort map {length} @data)[-1];

Too bad Perl doesn't have a built-in "max" that took a list parameter...
Since there's probably one in a package somewhere, that would make the above
more compact, and probably more efficient (or, of course, you could probably
write one in another single line of code :-).

 - Andy Jacobs

-----Original Message-----
From: owner-fwp@technofile.org [mailto:owner-fwp@technofile.org]On
Behalf Of Larry Rosler
Sent: Wednesday, June 23, 1999 5:29 PM
To: 'fwp@technofile.org'
Subject: [FWP] Finding the length of the longest string


The obvious (dull) way:

my $len = 0;
$len < length and $len = length
    for @data;

Subtler and somewhat faster:

my $len = "";
$len ^= $_ for @data;
$len = length $len;

Any better ideas?

-- 
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

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