in reply to Max value

Thank you for the replies. It helps a lot. I have one more question regarding finding the max value.

I had stored an array @arrayh = 3 3 4 4 5 5 and another array name @wire = N10 N11 N16 N19 N22 N23 .

With that, I am finding the max value in @arrayh which is 5, 5. When I print, how do I print the value selected and tally it with the same location as the one in @wire?

Meaning that when the max value is both 5,5, it will display the wire so N22 = 5, and N23 = 5.

for(my $i=0; $i < @arrayh; $i++) { my $max = max @arrayh; my $add = 1; print @arrayh,"\n"; print @wire; print $i+1, ". Hardest to control to logic 0 is at $wire[$i] with +value $max \n"; }

Replies are listed 'Best First'.
Re^2: Max value
by Laurent_R (Canon) on Apr 17, 2019 at 07:43 UTC
    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[0]; 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.
      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[0]; 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;
Re^2: Max value
by Marshall (Canon) on Apr 19, 2019 at 05:14 UTC
    The easy thing to do is generate a hash and then use the code that you already have to print N22=>5 N23=>5.
    Adapt my previous post to allow multiple max values to be printed however you want.
    #!/usr/bin/perl use strict; use warnings; my @wire = qw(N10 N11 N16 N19 N22 N23); my @arrayh = qw(3 3 4 4 5 5); my %hash; @hash{@wire}=@arrayh; #hash slice foreach my $key (keys %hash) { print "$key => $hash{$key}\n"; } __END__ N23 => 5 N19 => 4 N10 => 3 N16 => 4 N22 => 5 N11 => 3