gpt4 book ai didi

perl - 如何在 Perl 语句中获取输入文件的名称?

转载 作者:行者123 更新时间:2023-12-02 07:50:52 26 4
gpt4 key购买 nike

文件monday.csv

223.22;1256.4
227.08;1244.8
228.08;1244.7
229.13;1255.0
227.89;1243.2
224.77;1277.8

文件tuesday.csv

227.02;1266.3
227.09;1234.9
225.18;1244.7
224.13;1255.3
228.59;1263.2
224.70;1247.6

此 Perl 单行代码为我提供了文件“monday.csv”中第一列中前三位数字为 227 或 226 的行中第二列中具有最高值的行:

perl -F\; -ane '$hash{$_} = $F[1] if /22[78]/; END{ print and exit for sort{ $hash{$b} <=> $hash{$a} } keys %hash }' monday.csv

此 Perl 单行代码为我提供了所有 *day.csv 文件中第一列中前 3 位数字为 227 或 226 的行中第二列中具有最高值的行:

perl -F\; -ane '$hash{$_} = $F[1] if /22[78]/; END{ print and exit for sort{ $hash{$b} <=> $hash{$a} } keys %hash }' *day.csv

我怎样才能重写这个单行代码以获得类似的输出

filename : "row with the highest value in the second column from the rows where in the first column the first 3 digits are 227 or 226 from the file 'filename.csv'"

对于每个*day.csv 文件?

最佳答案

您可以使用$ARGV作为当前文件名。如果您只对最大值感兴趣,则无需存储所有值然后对它们进行排序;相反,只需存储每个文件的最大值。另外,您的正则表达式可能应该锚定到行的开头。

# Line breaks added for display purposes.
perl -F\; -ane '
$max{$ARGV} = $F[1] if /^22[78]/ and $F[1] > $max{$ARGV};
END{ print "$_\t$max{$_}" for sort keys %max}
' *day.csv

或者,如果您想存储最大值出现的整行:

perl -F\; -ane '
($max{$ARGV}{ln}, $max{$ARGV}{mx}) = ($_, $F[1])
if /^22[78]/ and $F[1] > $max{$ARGV}{mx};
END{ print "$_\t$max{$_}{ln}" for sort keys %max}
' *day.csv

关于perl - 如何在 Perl 语句中获取输入文件的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3948393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com