gpt4 book ai didi

perl - 跳出 foreach 循环后,如何重新启动 "do-while"循环?

转载 作者:行者123 更新时间:2023-12-03 22:52:52 24 4
gpt4 key购买 nike

我的配置脚本中有一小段代码,想法是加载配置然后检查每个键是否已输入主机名。但是,如果发现配置包含相同的主机名,则它会被拒绝并显示一条警告消息,表明该主机名的配置已经存在。

问题是我需要检查散列键是否存在的 foreach 循环来重新启动 do-while 循环,以便可以尝试另一个主机名或用户可以 ^C出剧本。

这是片段;

my $host;
do {
print "Enter the hostname or IP of the ESXi server: ";
chomp($host = <STDIN>);

if ($host eq '') {
print "You must enter a hostname or IP address!\n";
} elsif ($host ne '') {

# We need to catch duplicate configurations for we don't do the same work twice
foreach (keys %config) {
if ($config{$_}{host} ne $host) {
last;
} elsif ($config{$_}{host} eq $host) {
warn "Configuration for $host already exists!\n";
}
}

if ($ping_obj->ping($host)) {
$config{$config_tag}{host} = $host;
} elsif (! $ping_obj->ping($host)) {
print RED . "Ping test for \'$host\' failed" . RESET . "\n";
}

$ping_obj->close();
}
} while ($config{$config_tag}{host} eq 'undef');

这就是模板哈希的样子。
my %template = (
host => 'undef',
port => 'undef',
login => {
user => 'undef',
password => 'undef',
},
options => {
snapshots => "0",
compress => "0",

# This is expressed as an array
exclude => 'undef',
},
);

最佳答案

如果有一个 goto LABEL 的用途Perl 中的语句,就是这样。

do {
START: # could also go right before the "do"
...
if (...) {
warn "Configuration exists. Start over.\n";
goto START;
}
} while (...);

关于perl - 跳出 foreach 循环后,如何重新启动 "do-while"循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8597973/

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