gpt4 book ai didi

perl - 在 Perl 中删除部分字符串

转载 作者:行者123 更新时间:2023-12-01 07:15:49 25 4
gpt4 key购买 nike

my $input1 = "hours";
my $input2 = "Total hours";
my ($matching_key) = grep { /$input2/ } $input1;
print "Matching key :: $matching_key";

我想要的是从 $input2 中去除“Total”并将其分配回 input2 以便我的 grep 行匹配。我怎样才能去掉那个词?

最佳答案

如果我正确理解你的问题,你想要

$input2 =~ s/Total //;

但是,您也滥用了 grep() ;它在其第二个参数(也应该是一个列表)中返回一个元素数组,该数组与第一个参数中给出的模式相匹配。虽然它实际上会在标量上下文中以您使用它的方式返回“小时”,但这几乎是巧合。

关于perl - 在 Perl 中删除部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6863407/

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