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

坚强是说给别人听的谎言 提交于 2019-11-25 23:37:55

问题


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, $msg4) if @val4;
printmsg(@val5, $msg5) if @val5;
printmsg(@val6, $msg6) if @val6;

So i wrote the following snippet

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

It doesn\'t work and breaks out with errors.


回答1:


Whenever you find yourself postfixing variable names with an integer index, realize that you should have used an array instead:

my @msgs = ('msg1', 'msg2', ..., 'msg6');
my @vals = ( [ @val1 ], [ @val2 ], ..., [ @val6 ] );

See also the FAQ How can I use a variable as a variable name?

As the answer to the FAQ notes, if the variables are not indexed by an integer, you can use a hash table:

By using symbolic references, you are just using the package's symbol-table hash (like %main::) instead of a user-defined hash. The solution is to use your own hash or a real reference instead.

$USER_VARS{"fred"} = 23;
my $varname = "fred";
$USER_VARS{$varname}++;  # not $$varname++

You should read the entire FAQ list at least once a year.

Update: I purposefully left symbolic references out of my answer because they are unnecessary and likely very harmful in the context of your question. For more information, see Why it's stupid to 'use a variable as a variable name'?, part 2 and part 3 by mjd.




回答2:


You can't just string variables together like that and get a resulting variable. You COULD evaluate the expression of $msg + i, but it's probably better if you make msg an array and just index: $msg[$i].




回答3:


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?



来源:https://stackoverflow.com/questions/1549685/how-can-i-use-a-variable-as-a-variable-name-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!