Ram*_*kar 0 arrays variables perl
我想在@array_mem_depth_$i 中使用 $i 值,以便我动态创建的数组看起来像@array_mem_depth_1024,@array_mem_depth_512。
如何实现这一目标?
while(<FH>) {
$line = $_;
chomp($line);
foreach my $i (@depth_uarr) { if ( $line =~ /$i/) {push (@array_mem_depth_${i}, $line);} }
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我实际上不建议使用具有动态名称的变量。虽然这在技术上可能是可行的,但这会使您的代码更加复杂,没有明显的好处。
相反,为什么没有数组引用的散列。根据您的部分代码,这看起来像:
my %h;
while(my $line = <FH>) {
chomp($line);
foreach my $i (@depth_uarr) {
if ( $line =~ /$i/ ) {
push @{ $h{$i} }, $line;
}
}
}
Run Code Online (Sandbox Code Playgroud)