 The stupid question is the question not asked PerlMonks

### Re^2: Max value

by Laurent_R (Canon)
 on Apr 17, 2019 at 07:43 UTC ( #1232702=note: print w/replies, xml ) Need Help??

in reply to Re: Max value

Managing two arrays in parallel is probably not the best design. Some other data structure, such for example as an array of hashes, would probably be better.

But anyway, you can loop over the indices of the first array and keep track of the index corresponding to the max value, and then use the max index in the other array. Something like this (untested):

```my \$max_i;
my \$max = \$arrayh;
for my \$i (0..\$#arrayh) {
if (\$arrayh[\$i] > \$max) {
\$max = \$arrayh[\$i];
\$max_i = \$i;
}
}
print " Max is: \$wire[\$max_i]\n";
A couple of changes would be required if you can have several maximum values (such as using an array of the indices with the max value and adding a test for equality), but this is left as an exercise to the reader.

Replies are listed 'Best First'.
Re^3: Max value
by ameezys (Acolyte) on Apr 17, 2019 at 09:02 UTC
A couple of changes would be required if you can have several maximum values (such as using an array of the indices with the max value and adding a test for equality), but this is left as an exercise to the reader.

Mind showing an example for that? I still don't understand how to do it.

Perhaps something like this (still untested):
```my @max_i;
my \$max = \$arrayh;
for my \$i (0..\$#arrayh) {
if (\$arrayh[\$i] > \$max) {
\$max = \$arrayh[\$i];
@max_i = (\$i);
} elsif (\$arrayh[\$i] == \$max) {
push @max_i, \$i;
}
}
print " Max values are: \n"
print "- \$wire[\$_]\n" for @max_i;

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-10-28 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My first memorable Perl project was:

Results (95 votes). Check out past polls.

Notices?