Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > length of the longest $_ in @_

Reply
Thread Tools

length of the longest $_ in @_

 
 
Sam
Guest
Posts: n/a
 
      09-22-2003
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

 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      09-22-2003
On Mon, 22 Sep 2003 08:22:03 GMT, Sam <(E-Mail Removed)>
wrote:

>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=$_;
}

 
Reply With Quote
 
 
 
 
Vlad Tepes
Guest
Posts: n/a
 
      09-22-2003
Sam <(E-Mail Removed)> wrote:

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

 
Reply With Quote
 
Barry Kimelman
Guest
Posts: n/a
 
      09-22-2003
[This followup was posted to comp.lang.perl.misc]

In article <(E-Mail Removed)>, samj2
@austarmetro.com.au says...
> 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];
 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      09-22-2003
On Mon, 22 Sep 2003, Vlad Tepes wrote:

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

--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

 
Reply With Quote
 
Vlad Tepes
Guest
Posts: n/a
 
      09-22-2003
Jeff 'japhy' Pinyan <(E-Mail Removed)> wrote:

> On Mon, 22 Sep 2003, Vlad Tepes wrote:
>
>> 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.


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.

--
Vlad
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-22-2003
Purl Gurl <(E-Mail Removed)> wrote in comp.lang.perl.misc:

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

 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      09-22-2003
Matija Papec wrote:
> On Mon, 22 Sep 2003 08:22:03 GMT, Sam <(E-Mail Removed)>
> wrote:
>
>
>>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=$_;
> }
>



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;

 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      09-22-2003
Barry Kimelman wrote:
> [This followup was posted to comp.lang.perl.misc]
>
> In article <(E-Mail Removed)>, samj2
> @austarmetro.com.au says...
>
>>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

 
Reply With Quote
 
Eric Bohlman
Guest
Posts: n/a
 
      09-22-2003
Matija Papec <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

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


$max=$_ if $_>$max;

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

> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Length of longest contiguous digits exercise Csaba Gabor Javascript 19 10-26-2009 01:37 PM
Longest Sites Name With Worlds Longest Stuffs... Vijay Murthy Computer Information 0 02-04-2006 09:57 AM
How do I find the length of the longest line in a file? John Howard Perl Misc 14 08-19-2005 02:49 PM
System.ArgumentOutOfRangeException: Length cannot be less than zero. Parameter name: length =?Utf-8?B?SG96aQ==?= ASP .Net 1 06-01-2004 11:06 PM
How to get length of string? length() problems Mitchua Perl 5 07-17-2003 12:08 AM



Advertisments