Using a dynamically generated variable name in Perl's strict mode [duplicate]

落爺英雄遲暮 提交于 2019-12-19 19:48:40

问题


Basically, I would like to grab the contents of a variable named in a dynamically generated string, but all efforts to accomplish this in strict mode have failed. There are several posts about similar problems, but none seem to have solutions that have worked for me.

This is what I want to do:

# Fields:
$q1 = "ex. data 1";
$q2 = "ex. data 2";
$q3 = "ex. data 3";
$q4 = "ex. data 4";
$q5 = "ex. data 5";

# retrieve the desired field name.  q1, q2, q3, q4, or q5.
$field_name = fetch_the_desired_field_name();

# fetch the contents of the named field.  ex. data 1, ex. data 2, etc.
$contents_of_desired_field = $$field_name;

print $contents_of_desired_field;

Is there a way to do this in strict mode? In other posts about similar problems, people say that hashes are the answer, but I can't seem to quite grasp how to do this with a hash. It goes without saying that this is a very simplified example. In the actual code, there are 115 possible field names, all pulled from a database, with unpredictable contents.

来源:https://stackoverflow.com/questions/17434333/using-a-dynamically-generated-variable-name-in-perls-strict-mode

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