perl查找max并记录它是哪一个

perl查找max并记录它是哪一个,perl,max,Perl,Max,如果我有三个带整数的变量,并且想找出哪一个是最大的(不仅仅是最大整数),例如,如果a是3,b是4,c是5,我想知道c是最大的,而不是返回a 5给我。如何实现这一点或我应该使用 use List::Util $d = max($a,$b,$c); if($d == $a){} elsif($d == $b){} else{} 将值存储在数组中 循环数组中的每个索引(提示:使用0...$arrayNameconstruct) 在单独的两个变量中保留,$current\u max\u value

如果我有三个带整数的变量,并且想找出哪一个是最大的(不仅仅是最大整数),例如,如果a是3,b是4,c是5,我想知道c是最大的,而不是返回a 5给我。如何实现这一点或我应该使用

use List::Util

$d = max($a,$b,$c);
if($d == $a){}
elsif($d == $b){}
else{}
  • 将值存储在数组中

  • 循环数组中的每个索引(提示:使用
    0...$arrayName
    construct)

  • 在单独的两个变量中保留,
    $current\u max\u value
    $current\u max\u index

  • 当发现一个值大于
    $current\u max\u value
    时,将其存储在
    $current\u max\u value
    中,并将当前索引存储在
    $current\u max\u index

  • 循环完成后,您找到了最大元素的索引(
    $current\u max\u index

      • 将值存储在数组中

      • 循环数组中的每个索引(提示:使用
        0...$arrayName
        construct)

      • 在单独的两个变量中保留,
        $current\u max\u value
        $current\u max\u index

      • 当发现一个值大于
        $current\u max\u value
        时,将其存储在
        $current\u max\u value
        中,并将当前索引存储在
        $current\u max\u index

      • 循环完成后,您找到了最大元素的索引(
        $current\u max\u index


      通过使用单独的变量,几乎可以使任何事情都变得不可能。假设您使用的是数组

      my @a = (3,4,5);
      
      my $max_idx = 0;
      for my $idx (1..$#a) {
         $max_idx = $idx
            if $a[$idx] > $a[$max_idx];
      }
      
      say $max_idx;
      say $a[$max_idx];
      

      通过使用单独的变量,几乎可以实现任何不可能的事情。假设您使用的是数组

      my @a = (3,4,5);
      
      my $max_idx = 0;
      for my $idx (1..$#a) {
         $max_idx = $idx
            if $a[$idx] > $a[$max_idx];
      }
      
      say $max_idx;
      say $a[$max_idx];
      
      这很容易使用,即使对于非常大的数据集也是如此

      #!/usr/bin/env perl
      
      use strict;
      use warnings;
      
      use PDL;
      
      my $pdl = pdl( 3,4,5 );
      my (undef, $max, undef, $max_index) = $pdl->minmaximum;
      
      print "Max: $max (at index $max_index)\n";
      
      这很容易使用,即使对于非常大的数据集也是如此

      #!/usr/bin/env perl
      
      use strict;
      use warnings;
      
      use PDL;
      
      my $pdl = pdl( 3,4,5 );
      my (undef, $max, undef, $max_index) = $pdl->minmaximum;
      
      print "Max: $max (at index $max_index)\n";