开发者

Use Join perl function to create variables

开发者 https://www.devze.com 2023-03-26 04:48 出处:网络
I am trying to do some thing l开发者_运维知识库ike this: my @Amode=(\'1\',\'2\',\'3\'); my @Bmode=(\'1\',\'2\',\'3\');

I am trying to do some thing l开发者_运维知识库ike this:

my @Amode=('1','2','3');
my @Bmode=('1','2','3');
my @Cmode=('1','2','3');
my @Atemp=('1','2','3');
my @Btemp=('1','2','3');
my @Ctemp=('1','2','3');

my @mode=('A','B','C');
foreach (@mode) {
    my $newmode = join("",$_,mode);
    my $newtemp = join("",$_,temp);
}

I want to access the @Amode information through $newmode. Is this possible?


I see what you are trying to do there, but honestly I think you are making it more convoluted than it needs to be.

Why not use hashes?

my $modes = {
   'A' => [1,2,3],
   'B' => [1,2,3],
   'C' => [1,2,3],
};
foreach my $mode (keys %$modes){
    ... do something with $modes->{$mode};
}


You can't string together variable names, but you can make hash keys and access them.

E.g.

my %data = ( "A" => \@Amode, "B" => \@Bmode, "C" => \@Cmode );
my @mode = ("A", "B", "C");

for (@mode) {
    print @{$data{$_}};
}
0

精彩评论

暂无评论...
验证码 换一张
取 消