eou*_*uti 4 arrays variables perl
我必须存储使用Parallel :: ForkManager运行的请求中的数据.
但它不按顺序,我的意思是,我轮询了一个交换机的所有端口,但其中一些回答比其他端口更快.所以,我不知道如何保存它,以后再显示它.
我能这样做吗?
my @array = ();
$array[10] = "i am a string corresponding to port 10"
$array[2] = "the one for the port 2"
...
print @array;
Run Code Online (Sandbox Code Playgroud)
或者我应该使用带有端口数的%hash作为键,但它似乎不是最好的.
谢谢.
你可以这样做:
my @array = ();
$array[10] = "i am a string corresponding to port 10"
$array[2] = "the one for the port 2"
print @array;
Run Code Online (Sandbox Code Playgroud)
但是如果某些端口没有响应,则在阵列的插槽中将有未填充的undef条目.如你所说,使用哈希会更干净:
my %hash;
$hash{10} = "I am port 10";
$hash{2} = "I am port 2";
foreach my $key (keys %hash) {
print "$key: $hash{$key}\n";
}
Run Code Online (Sandbox Code Playgroud)
在Perl中,您不必担心数组的大小.他们根据需要成长:
my @array; # defines an empty array
$array[0] = 4; # array has one element
$array[9] = 20; # array has 10 elements now
print join "-", @array;
# prints: 4--------20 because the empty places are undef
Run Code Online (Sandbox Code Playgroud)
如果您有许多端口并且担心有太多空条目,请使用哈希.我看不出有任何理由不去做.