gpt4 book ai didi

perl - 在 perl 中连接字符串时出现意外结果

转载 作者:行者123 更新时间:2023-12-05 00:30:21 25 4
gpt4 key购买 nike

我正在尝试执行以下脚本,但没有获得所需的输出。似乎它仍在保留旧值并继续连接它。

./test.pl -v v10,v11,v12

实际结果 => 'v10','v11','v10','v12','v10','v11','v10'

预期结果 => 'v10','v11','v12'

你能帮忙解决这个问题吗?

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

my ($versionFlag,$finalVersionList);

sub validateInputAndFilter
{
my ($passedVersionList) = @_;
$finalVersionList ="";

my @versionList = split(",",$passedVersionList);
foreach my $ver (@versionList)
{
if($ver =~ m/^v[0-9][0-9]$/)
{
$finalVersionList .= "'$ver',$finalVersionList";
}
else
{
print "\nWARNING: Passed version *** $ver *** is NOT in expected format...skipping...\n";
}
}
$finalVersionList =~ s/\,$//;
print "\n\nfinalVersionList => $finalVersionList\n\n";
}

GetOptions('v|version=s' => \$versionFlag) || die "Please pass version list separated by comma";

if(defined $versionFlag)
{
&validateInputAndFilter("$versionFlag");
}
else
{
die "Please pass version list separated by comma";
}
exit 0;

最佳答案

这个:

$finalVersionList .= "'$ver',$finalVersionList";

应该只是:
$finalVersionList .= "'$ver,'";
$foo .= $bar ;相当于:
$foo = $foo . $bar;

所以, $finalVersionList .= "'$ver',$finalVerionList";相当于:
$finalVersionList = $finalVersionList . "'$ver',$finalVersionList";

关于perl - 在 perl 中连接字符串时出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16175144/

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