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

I

IJALAB

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

Jürgen Exner

IJALAB said:
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
 
S

sln

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
 
S

sopan.shewale

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





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?

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
 
S

sln

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?

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
 
J

John Bokma

Tad McClellan said:
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)
 
J

Jürgen Exner

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

John Bokma

Tad McClellan said:
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)
 
J

John Bokma

Xho Jingleheimerschmidt said:
Jürgen Exner said:
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?

Yup, and my experience is that good Perl runs fast enough. I suspect
Python to be a bit slower, but note that that's just a suspicion. On top
of that I don't care.
 
J

Jürgen Exner

Xho Jingleheimerschmidt said:
Jürgen Exner said:
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?

Well, yeah, so? Are you implying that people should use inefficent
algorithms when programming in Perl?

Of course the cost for designing, writing, and maintaining the code is
an important factor, too, and it often justifies the use of a less
efficient algorithm.
However in this case where there are trivial, short and well-known
algorithms for computing the largest element there is really no excuse
for using a poor algorithm(*).

*: Well, except that Tad noticed the "only 4 element list" and thus
efficieny is REALLY totally irrelevant.

jue
 
U

Uri Guttman

JE> Xho Jingleheimerschmidt said:
Jürgen Exner said:
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?

JE> Well, yeah, so? Are you implying that people should use inefficent
JE> algorithms when programming in Perl?

JE> Of course the cost for designing, writing, and maintaining the
JE> code is an important factor, too, and it often justifies the use
JE> of a less efficient algorithm. However in this case where there
JE> are trivial, short and well-known algorithms for computing the
JE> largest element there is really no excuse for using a poor
JE> algorithm(*).

JE> *: Well, except that Tad noticed the "only 4 element list" and thus
JE> efficieny is REALLY totally irrelevant.

so you think applying Sort::Maker on this list is not worth the effort?
:)

uri
 
X

Xho Jingleheimerschmidt

John said:
Xho Jingleheimerschmidt said:
Jürgen Exner said:
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?

Yup, and my experience is that good Perl runs fast enough.

Well, that really depends on fast enough for what. On occasion I've had
to rewrite certain parts in C to get the required speed, but that is
rare. And more to my point, my experience is that using sort is also
fast enough, at least in the context where Perl itself is fast enough.
I now use List::Util::max or min, but for stylistic reasons, not
efficiency reasons.
I suspect
Python to be a bit slower, but note that that's just a suspicion. On top
of that I don't care.

Right.

Xho
 
X

Xho Jingleheimerschmidt

Jürgen Exner said:
Xho Jingleheimerschmidt said:
Jürgen Exner said:
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?

Well, yeah, so? Are you implying that people should use inefficent
algorithms when programming in Perl?

Depends on in what domain it is inefficient.
Of course the cost for designing, writing, and maintaining the code is
an important factor, too, and it often justifies the use of a less
efficient algorithm.

Right, especially the difference in efficiency is factor of lnN, rather
than N or some higher order.

However in this case where there are trivial, short and well-known
algorithms for computing the largest element there is really no excuse
for using a poor algorithm(*).

There is no such thing as a trivial algorithm. I've seen people screw
up even this one, no matter how short and well-known it may be.


Xho
 
J

John Bokma

Xho Jingleheimerschmidt said:
John said:
Xho Jingleheimerschmidt said:
Jürgen Exner 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?

Yup, and my experience is that good Perl runs fast enough.

Well, that really depends on fast enough for what.

For the stuff I do with it, hence "my experience is". ;-). (Mostly
processing a lot of data)
On occasion I've
had to rewrite certain parts in C to get the required speed, but that
is rare. And more to my point, my experience is that using sort is
also fast enough, at least in the context where Perl itself is fast
enough. I now use List::Util::max or min, but for stylistic reasons,
not efficiency reasons.

Yup, that was more or less my point.

No, really. For (most if not all of) my stuff the speed of Perl/Python
is more than enough. Perl is often able to surprise me in a very
pleasant wa,y speedwise :).
 
S

sln

Xho Jingleheimerschmidt said:
John said:
Jürgen Exner 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?

Yup, and my experience is that good Perl runs fast enough.

Well, that really depends on fast enough for what.

For the stuff I do with it, hence "my experience is". ;-). (Mostly
processing a lot of data)

Regardless of what the OP literally said,
you would recommend sort to find the min or max value?

-sln
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top