【问题标题】:Perl get lowest number from array [closed]Perl从数组中获取最小的数字[关闭]
【发布时间】:2021-03-14 20:41:54
【问题描述】:

你好,我想在这里获得最低的成本是我的 json

[
'ups_standard_international|23.63',
'ups_worldwide_saver|20.8',
'ups_worldwide_express|21.11',
'ups_worldwide_expedited|18.75',
'usps_first_class_package_international|33.43',
'usps_priority_mail_international|42.34',
'usps_priority_mail_express_international|61.79'
];

我希望它显示 ups_worldwide_expedited|18.75

因为这是最低的成本。

谢谢。

【问题讨论】:

  • 你试过什么?它是如何失败的?
  • 你想解决这个问题的方法取决于你找到最大值后是否需要参考数据。如果除了解析它来找到最大值之外你不需要做任何事情,那么你就不需要显然是哈希的东西。如果您对解析后的值有很多其他访问权限,那么散列似乎是理想的。
  • 另外,通过“json”,你的意思是这是一个字符串吗?还是您已经将其解析为数组引用?
  • 这不是问题。这是免费工作的请求。

标签: json perl


【解决方案1】:

这里是您在 Perl 中执行此操作所需的信息。它实际上是 1 到 3 行代码。如果之后您有特定的 Perl 问题,请提出一个新问题。

假设您有一个 JSON 文件或文本 sn-p,请使用 JSON 模块对其进行解析。 https://metacpan.org/pod/JSON

在 Perl 中将其放入数组后,使用 foreach 遍历每个元素,并使用 split 或正则表达式解析成本字段。

如果成本小于当前成本,则只需将行和成本分配给变量。预先将成本变量初始化为不可能大的值,例如 1E9。循环结束后,打印出 line 变量。

这里是 Perl 文档的链接。
https://perldoc.perl.org/perl#Tutorials

你会发现 perlfunc 和 perlop 特别有用。

【讨论】:

    【解决方案2】:

    您可以使用哈希来跟踪迭代值时看到的最低成本

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $aref = [
        'ups_standard_international|23.63',
        'ups_worldwide_saver|20.8',
        'ups_worldwide_express|21.11',
        'ups_worldwide_expedited|18.75',
        'usps_first_class_package_international|33.43',
        'usps_priority_mail_international|42.34',
        'usps_priority_mail_express_international|61.79'
    ];
    
    my %lowest;
    
    foreach (@$aref) {
        my ( undef, $cost ) = split /[|]/;
        if ( !%lowest || $cost < $lowest{cost} ) {
            $lowest{cost} = $cost;
            $lowest{line} = $_;
        }
    }
    
    print $lowest{line}, "\n";
    

    编辑:我可能误解了 OP 的问题。 如果问题中“json”的sn-p不是Perl对象,而是一个原始的JSON字符串,我们可以使用JSON模块来做​​基本相同的事情,这里是上面代码的更新版本:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use JSON qw(decode_json);
    
    my $json = <<'END_JSON';
    [
        'ups_standard_international|23.63',
        'ups_worldwide_saver|20.8',
        'ups_worldwide_express|21.11',
        'ups_worldwide_expedited|18.75',
        'usps_first_class_package_international|33.43',
        'usps_priority_mail_international|42.34',
        'usps_priority_mail_express_international|61.79'
    ]
    END_JSON
    
    # First, use double quotes to quote the strings above, to make it valid JSON:
    $json =~ s/^(\s*)'([^']+)'(,?)\s*$/$1"$2"$3/gms;
    
    # Now decode the JSON above into a perl array
    my @array = @{ decode_json($json) };
    
    # This hash will hold the element with the lowest cost
    my %lowest;
    
    # While the array still has elements, take one line from it
    while ( my $line = shift @array ) {
    
        # Extract the cost from the line
        my ( undef, $cost ) = split /[|]/, $line, 2;
    
        # If this cost is the lowest we've seen yet, put it in the %lowest hash
        if ( !%lowest || $cost < $lowest{cost} ) {
            $lowest{cost} = $cost;
            $lowest{line} = $line;
        }
    }
    
    print $lowest{line}, "\n";
    
    

    【讨论】:

    • OP 询问的是 json 对象(文本),而不是 Perl 数组。
    • @TLP 我刚刚查看了 OP 发布的 sn-p,在我看来它更像是 Perl 匿名数组引用而不是 JSON 数组(JSON 字符串使用双引号,而不是单引号,最后有一个分号)。无论如何,我更新了我的答案以反映这一点。
    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多