Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      05-07-2010
IJALAB <> 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
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      05-07-2010
On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <> 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

 
Reply With Quote
 
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, s...@netherlands.com wrote:
> On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <balaji.d...@gmail.com> 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


 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      05-07-2010
On Fri, 7 May 2010 13:33:10 -0700 (PDT), "" <> wrote:

>On May 7, 11:56*pm, s...@netherlands.com wrote:
>> On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <balaji.d...@gmail.com> 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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      05-07-2010
Tad McClellan <> writes:

> IJALAB <> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-08-2010
"" <> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-08-2010
Tad McClellan <> wrote:
>John Bokma <> wrote:
>> Tad McClellan <> writes:
>>
>>> IJALAB <> 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
 
Reply With Quote
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      05-08-2010
Jürgen Exner wrote:
> "" <> 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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      05-08-2010
Tad McClellan <> writes:

> John Bokma <> wrote:
>> Tad McClellan <> writes:
>>
>>> IJALAB <> 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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is there a simple way to find the list index to the max value? W. eWatson Python 8 02-17-2010 04:44 AM
find max numeric value with LINQ John ASP .Net 0 10-27-2008 07:58 PM
STL algorithm to find max value in a set or map? cayblood C++ 5 11-03-2005 06:57 PM
Max Apperture and Max. Shutter Speed Confusion-HELP bhaskar Digital Photography 12 07-22-2003 05:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57