length of the longest $_ in @_

S

Sam

Hello .... again :)
after I search my book and the online hlep. trying to find it there is a
built-in function to get the length of the longest item in a givin
array. length will not do it and scalar @array will not do it. I can
loop through the array but just wanted to ask if there is a built in
function for it. and how would you know if there is a built-in-function
for a task you want to do? or is it by time-experiance combo?

thanks
 
M

Matija Papec

Hello .... again :)
after I search my book and the online hlep. trying to find it there is a
built-in function to get the length of the longest item in a givin
array. length will not do it and scalar @array will not do it. I can
loop through the array but just wanted to ask if there is a built in
function for it. and how would you know if there is a built-in-function
for a task you want to do? or is it by time-experiance combo?

You'll have to loop through the array,
#untested
my $max = 0;
$_>$max && $max=$_ for map length, @arr;


or if you more prefer,
for (map length, @arr) {
$_>$max && $max=$_;
}
 
V

Vlad Tepes

Sam said:
after I search my book and the online hlep. trying to find it there is
a built-in function to get the length of the longest item in a givin
array.

No builtin, but it's quite easy to make one (this one's a little
contorted to handle references to arrays):

sub amax(@;\$) { # prototype to make it work like built-in (no parens)
my $max = 0;
map { $max = length if $max < length }
ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_;
return $max;
}

my @ary = qw( one two three four five );

printf "Max length in array: %d\n", amax @ary;
printf "Max length in aref : %d\n", amax \@ary;

length will not do it and scalar @array will not do it.

Nope, length measures the length of a string, and scalar @array returns
number of elements in the array.
and how would you know if there is a built-in-function for a task you
want to do? or is it by time-experiance combo?

You have to read about the built-ins and remember them, or look them up:

perldoc perlfunc

Nice thing is that the functions are categorised. For example, here's
the built-ins that applies to arrays:

Functions for real @ARRAYs
"pop", "push", "shift", "splice", "unshift"

Read and re-read perlfunc.
All experts do it from time to time.

Cheers,
 
B

Barry Kimelman

[This followup was posted to comp.lang.perl.misc]

samj2 said:
Hello .... again :)
after I search my book and the online hlep. trying to find it there is a
built-in function to get the length of the longest item in a givin
array. length will not do it and scalar @array will not do it. I can
loop through the array but just wanted to ask if there is a built in
function for it. and how would you know if there is a built-in-function
for a task you want to do? or is it by time-experiance combo?

thanks

$maxlen = (sort { $b <=> $a } map { length $_ } @names)[0];
 
J

Jeff 'japhy' Pinyan

sub amax(@;\$) { # prototype to make it work like built-in (no parens)

What exactly is that prototype doing? And you don't need a prototype to
get the function to work without parens -- it merely must be declared (or
defined) before it's used.
 
V

Vlad Tepes

Jeff 'japhy' Pinyan said:
What exactly is that prototype doing? And you don't need a prototype to
get the function to work without parens -- it merely must be declared (or
defined) before it's used.

I wanted to make the sub accept either an array reference or an array.
A quick look at perlsub suggest that it doesn't work this way.

I have to take a closer look at perlsub again. I haven't got time right
now, though.
 
A

Anno Siegel

Purl Gurl said:
#!perl

@Array = qw (three two one);

@Array = sort { length ($a) cmp length ($b) } @Array;

print "Longest Element: $Array[$#Array]";


PRINTED RESULTS:
________________

Longest Element: three

Now change the array:

my @Array = qw (three two one hundredfourtyfive);

It still prints "three".

Anno
 
S

Sam

Matija said:
You'll have to loop through the array,
#untested
my $max = 0;
$_>$max && $max=$_ for map length, @arr;


or if you more prefer,
for (map length, @arr) {
$_>$max && $max=$_;
}


I have 10 arrays in my program and want to do this in all of them. my
approch would be

#to store the result of the loop in separate sclars for each array

my ($arr1_max, $arr2_max, $arr(n)_max)=0;

#is there a perl variable which holds a list of all the arrays in a
#program? instead of doing this comming line

for my $arr (\@arr1, \@arr2, \@arr(n)) {
my $uni_max = '$','the name of the variable inside the $arr in this
case arr1, how can I do this?','_max'; #builds a string which is the variable to hold
the result of the inside loop
for (map length, @{$arr}) {
$uni_max=$_ if $_>$uni_max;
}

print $arr1_max;
print $arr2_max;
print $arr(n)_max;
 
S

Sam

Barry said:
[This followup was posted to comp.lang.perl.misc]

samj2 said:
Hello .... again :)
after I search my book and the online hlep. trying to find it there is a
built-in function to get the length of the longest item in a givin
array. length will not do it and scalar @array will not do it. I can
loop through the array but just wanted to ask if there is a built in
function for it. and how would you know if there is a built-in-function
for a task you want to do? or is it by time-experiance combo?

thanks


$maxlen = (sort { $b <=> $a } map { length $_ } @names)[0];

that is very clean, thanks alot
 
U

Uri Guttman

this is from the current post by moronzilla:
^^^

this is from its first post in this thread:

@Array = sort { length ($a) cmp length ($b) } @Array;
^^^

PG> Would I practice deceit in this truly honest newsgroup?

editing quoted parts of your crap code won't wash as google has the
evidence and proof of your deceit and all your stupidity.

uri
 
A

Anno Siegel

Purl Gurl said:
Anno Siegel wrote:

Purl Gurl wrote:
@Array = qw (three two one);
@Array = sort { length ($a) <=> length ($b) } @Array;
print "Longest Element: $Array[$#Array]";
Now change the array:
my @Array = qw (three two one hundredfourtyfive);
It still prints "three".

It does?

It is "one-hundred-forty-five" for your interest.

Would I practice deceit in this truly honest newsgroup?

Good, you found the bug. Avoiding the mistake in the future would have
been enough. Correcting the past by changing the quoted text was not
required, and a bad move.

Anno
 
M

Matija Papec

X-Ftn-To: Eric Bohlman

Eric Bohlman said:
$max=$_ if $_>$max;

is more Perlish. Your form strikes me as writing sh in Perl.

I'm sorry. :) Actually the former doesn't compile so it must be,
$_>$max and $max=$_;
or
$max=$_ if $_>$max;
as you pointed out.
 
M

Matija Papec

X-Ftn-To: Sam

Sam said:
I have 10 arrays in my program and want to do this in all of them. my
approch would be

#to store the result of the loop in separate sclars for each array

my ($arr1_max, $arr2_max, $arr(n)_max)=0;

#is there a perl variable which holds a list of all the arrays in a
#program?

I'm afraid there is no such thing, but you can use array of arrays; I see
you're used to references,

#untested
my @arrofarr = (\@arr1, \@arr2, \@arr3);
my @max = (0) x (scalar @arrofarr);

for my $i (0 .. $#arrofarr) {

for (map length, @{ $arrofarr[$i] }) {
$max[$i]=$_ if $_>$max[$i];
}
}
print "Max lengths are: ", join ",", @max;

hope this helps.
 
C

Chief Squawtendrawpet

Barry said:
$maxlen = (sort { $b <=> $a } map { length $_ } @names)[0];
that is very clean, thanks alot

Sorting the entire list of lengths just to find the max seems like
overkill, at least if speed is an issue. The straightforward solution seems
best, and you can even type it all on one line if you want.

for (map length, @list){ $max = $_ if $_ > $max }

Chief S.
 
J

Jay Tilton

: after I search my book and the online hlep. trying to find it there is a
: built-in function to get the length of the longest item in a givin
: array. length will not do it and scalar @array will not do it. I can
: loop through the array but just wanted to ask if there is a built in
: function for it.

No, there is not. It is a useful thing to be able to do, but not so
useful to be worth consuming another keyword.

You can get there very easily with two built-in functions and one
bundled-in module.

use List::Util 'max';
$max = max( map length, @array );

: and how would you know if there is a built-in-function
: for a task you want to do?

Habitual reading of perlfunc.

: or is it by time-experiance combo?

Bingo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top