How can I use a variable as a variable name in Perl?

前端 未结 3 1372
离开以前
离开以前 2020-11-22 14:48

I need to achieve the following in perl

printmsg(@val1, $msg1) if @val1;
printmsg(@val2, $msg2) if @val2;
printmsg(@val3, $msg3) if @val3;
printmsg(@val4, $m         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 14:56

    If I understand, you need "eval"!

    for(my $i=1; $i < 6; $i++ ) {
      eval 'printmsg(@val'. $i . ', $msg' . $i .') if @val' . $i;
    }
    

    But remember! All variable(@val1, @val2, ..., @valN) must exists! As you're not providing too much of your code I'm unavaiable to infer more about this problem. Maybe you could provide more code huh?

提交回复
热议问题