zoukankan      html  css  js  c++  java
  • perl取数组中最值

    my @a=(11,22,33,44);

    my $minCnt = &min(@a);

    sub max       # 采用遍历算法。先将参数中的第一个值赋给$currentMaxCnt。 # @_ 是默认的包含本函数所有参数 [如(11,22,33)]的数组。
            # shift @_ 有两个结果: 1. 将数组 @_ 中的第一个值做为返回值(赋给了$currentMaxCnt). 2. 将@_数组第一个值弹出[此后@_的值变为(22,33)]. my $currentMaxCnt = shift @_; # 函数中使用shift时,@_可以省略。上面代码也可以写成这样。 # my $currentMaxCnt = shift;

    # 遍历整个@_数组。

    foreach ( @_ )

    {     # $_ 表示数组@_中当前被遍历到的元素.

    if ( $_ > $currentMaxCnt )

    {     # 如果发现当前数组元素比$currentMaxCnt大,那就将$currentMaxCnt重新赋值为当前元素。

    $currentMaxCnt = $_;

    } }     # 函数返回值为标量      $currentMaxCnt.

    return $currentMaxCnt;
    } print $maxCnt

     
    输出最小值:
    my @a=(11,22,33,44);
    my $minCnt = &min(@a);
    sub min { my $currentMinCnt = shift @_;
    foreach ( @_ )
    { if ( $_ < $currentMinCnt )
      {
      $currentMinCnt = $_;
      }
    }
    return $currentMinCnt;
    }
    print $minCnt
     
     
    或是:
    use List::Util qw(first max maxstr min minstr reduce shuffle sum);
  • 相关阅读:
    Windows10如何添加开机启动项
    selenium自动化文件上传、文件下载
    windows下创建虚拟环境
    selenium 处理js日历控件
    selenium鼠标和键盘事件
    selenium绕过某宝验证,手动验证滑块
    python国内镜像源
    Linux下jenking安装
    发送邮件
    基于python的selenium两种文件上传操作
  • 原文地址:https://www.cnblogs.com/blueicely/p/2816136.html
Copyright © 2011-2022 走看看