#!/usr/bin/perl
use strict;
use warnings;
my @alphanum_0 = (0, 0, 0);
my @alphanum_1 = (1, 1, 1, 1);
my @alphanum_A = ('A', 'A', 'A', 'A', 'A');
my @alphanum_B = ('B', 'B', 'B', 'B', 'B', 'B');
my $MyString = "01ab";
my @MyArray = split(//, uc($MyString));
my $ArrayRef = "\\\@alphanum_$MyArray[2]";
print "$ArrayRef\n";
&test_sub($ArrayRef, "myvar"); #this line does not work
&test_sub(\@alphanum_A, "myvar"); #this line works as my wish
sub test_sub {
my @tmp_array = @{$_[0]};
my $tmp_var = $_[1];
print "$#tmp_array, @tmp_array, $tmp_var\n"
}
如您所见,我需要通过变量分配输入数组。 我怎样才能达到我想要的效果?
答案 0 :(得分:5)
使用HoA
my %alphanum = (
0 => [0, 0, 0],
1 => [1, 1, 1, 1],
A => ['A', 'A', 'A', 'A', 'A'],
B => ['B', 'B', 'B', 'B', 'B', 'B'],
);
my $ArrayRef = $alphanum{ $MyArray[2] };