Velocity Reviews > Perl > how do i find the max value out of an array?

how do i find the max value out of an array?

IJALAB
Guest
Posts: n/a

 05-07-2010
Hi,

i have a text file which is comma seperated and i have extracted few
values from the text in an array for example,
30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38

my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
12....so on)
i have put a split statement and in a loop and captured these 4
elements in an array. how do i find the max of these values, which is
in an array using perl?

thanks

Jürgen Exner
Guest
Posts: n/a

 05-07-2010
IJALAB <(E-Mail Removed)> wrote:
>i have a text file which is comma seperated and i have extracted few
>values from the text in an array for example,
>30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38
>
>my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
>12....so on)
>i have put a split statement and in a loop and captured these 4
>elements in an array. how do i find the max of these values, which is
>in an array using perl?

Is this some kind of homework? This kind of algorithm is usually an
introductory example when discussing complex data structures and their
algorithms.

Standard way it to loop through the array and remember the (so far)
largest element..

Or you simply use List::Util.

jue

sln@netherlands.com
Guest
Posts: n/a

 05-07-2010
On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <(E-Mail Removed)> wrote:

>Hi,
>
>i have a text file which is comma seperated and i have extracted few
>values from the text in an array for example,
>30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38
>
>my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
>12....so on)
>i have put a split statement and in a loop and captured these 4
>elements in an array. how do i find the max of these values, which is
>in an array using perl?
>
>thanks

This would be my preferred method.
\$string = join '', <DATA>

-sln
---------------

use strict;
use warnings;

my \$string = q(30, 1, 4 ,5, -31, 4, 2, 3, 32, 2,3, 0, 38 , 39, 40 );
my (\$max,\$min) = (0,0);

for (split / (?: \s*,\s*[^,]*){3} \s* , \s* | \s*, .* \$/xs , \$string) {
\$max = \$_ unless \$max > \$_ ;
\$min = \$_ unless \$min < \$_ ;
}
print "min/max = \$min, \$max\n";

__END__

min/max = -31, 38

sopan.shewale@gmail.com
Guest
Posts: n/a

 05-07-2010
Once you have array, how about?
my \$max = (sort { \$b <=> \$a } @array)[0];

On May 7, 11:56*pm, (E-Mail Removed) wrote:
> On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <(E-Mail Removed)> wrote:
> >Hi,

>
> >i have a text file which is comma seperated and i have extracted few
> >values from the text in an array for example,
> >30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38

>
> >my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
> >12....so on)
> >i have put a split statement and in a loop and captured these 4
> >elements in an array. how do i find the max of these values, which is
> >in an array using perl?

>
> >thanks

>
> This would be my preferred method.
> \$string = join '', <DATA>
>
> -sln
> ---------------
>
> use strict;
> use warnings;
>
> my \$string = q(30, 1, 4 ,5, -31, 4, 2, 3, 32, 2,3, 0, 38 , 39, 40 );
> my (\$max,\$min) = (0,0);
>
> for (split / (?: \s*,\s*[^,]*){3} \s* , \s* | \s*, .* \$/xs , \$string) {
> * * \$max = \$_ unless \$max > \$_ ;
> * * \$min = \$_ unless \$min < \$_ ;}
>
> print "min/max = \$min, \$max\n";
>
> __END__
>
> min/max = -31, 38

sln@netherlands.com
Guest
Posts: n/a

 05-07-2010
On Fri, 7 May 2010 13:33:10 -0700 (PDT), "(E-Mail Removed)" <(E-Mail Removed)> wrote:

>On May 7, 11:56*pm, (E-Mail Removed) wrote:
>> On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <(E-Mail Removed)> wrote:
>> >Hi,

>>
>> >i have a text file which is comma seperated and i have extracted few
>> >values from the text in an array for example,
>> >30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38

>>
>> >my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
>> >12....so on)
>> >i have put a split statement and in a loop and captured these 4
>> >elements in an array. how do i find the max of these values, which is
>> >in an array using perl?

>>
>> >thanks

>>
>> This would be my preferred method.
>> \$string = join '', <DATA>
>>
>> -sln
>> ---------------
>>
>> use strict;
>> use warnings;
>>
>> my \$string = q(30, 1, 4 ,5, -31, 4, 2, 3, 32, 2,3, 0, 38 , 39, 40 );
>> my (\$max,\$min) = (0,0);
>>
>> for (split / (?: \s*,\s*[^,]*){3} \s* , \s* | \s*, .* \$/xs , \$string) {
>> * * \$max = \$_ unless \$max > \$_ ;
>> * * \$min = \$_ unless \$min < \$_ ;}
>>
>> print "min/max = \$min, \$max\n";
>>
>> __END__
>>
>> min/max = -31, 38

>
>
>
>
>>Once you have array, how about?

>my \$max = (sort { \$b <=> \$a } @array)[0];

Why get an array? But sure why not, I never
met a sort that uses boolean that I didn't like.

-sln

John Bokma
Guest
Posts: n/a

 05-07-2010

> IJALAB <(E-Mail Removed)> wrote:
>
>> captured these 4
>> elements in an array. how do i find the max of these values, which is
>> in an array using perl?

>
> my(\$max) = sort {\$b <=> \$a} @array;

use List::Util 'max';

my \$max = max @array;

A linear search /might/ be faster than sorting if @array is large.

See perldoc List::Util

(in my experience an under used module)

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development

Jürgen Exner
Guest
Posts: n/a

 05-08-2010
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
>Once you have array, how about?
>my \$max = (sort { \$b <=> \$a } @array)[0];

If you insist on being inefficient, then that is certainly a good
solution.
Sort is O(n*log n), while computing the max value can easily be done in
O(n).

jue

Jürgen Exner
Guest
Posts: n/a

 05-08-2010
>John Bokma <(E-Mail Removed)> wrote:
>> Tad McClellan <(E-Mail Removed)> writes:
>>
>>> IJALAB <(E-Mail Removed)> wrote:
>>>
>>>> captured these 4
>>>> elements in an array. how do i find the max of these values, which is
>>>> in an array using perl?
>>>
>>> my(\$max) = sort {\$b <=> \$a} @array;

>
>> A linear search /might/ be faster than sorting if @array is large.

>
>
>He said n=4, which is why I quoted where he said that n=4

Good catch, I missed it.

jue

Xho Jingleheimerschmidt
Guest
Posts: n/a

 05-08-2010
Jürgen Exner wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>> Once you have array, how about?
>> my \$max = (sort { \$b <=> \$a } @array)[0];

>
> If you insist on being inefficient,

We are discussing Perl, aren't we?

> then that is certainly a good
> solution.
> Sort is O(n*log n), while computing the max value can easily be done in
> O(n).

Xho

John Bokma
Guest
Posts: n/a

 05-08-2010

> John Bokma <(E-Mail Removed)> wrote:
>> Tad McClellan <(E-Mail Removed)> writes:
>>
>>> IJALAB <(E-Mail Removed)> wrote:
>>>
>>>> captured these 4
>>>> elements in an array. how do i find the max of these values, which is
>>>> in an array using perl?
>>>
>>> my(\$max) = sort {\$b <=> \$a} @array;

>
>> A linear search /might/ be faster than sorting if @array is large.

>
> He said n=4, which is why I quoted where he said that n=4

Yes, yes, I didn't miss this, hence the "/might/" and "large"

(Also, it was a bit of a plug for List::Util)

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development