Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 851
user2543941 Avatar asked Jul 02 '13 19:07

user2543941


People also ask

How do I create a dynamic variable in Perl?

We can generate dynamic variable names in Perl using Symbolic References. To use the feature, we have to turn off strict refs. The code below generates the variable names 'var1', 'var2', 'var3' dynamically in a loop as strings, names which can be used as actual variable names with the help of symbolic references.

What is $@ in Perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

How many types of built in variables are in Perl?

Hence, three types of variables will be used in Perl. A scalar variable can store either a number, a string, or a reference and will precede by a dollar sign ($). An array variable will store ordered lists of scalars and precede by @ sign.

What is Perl variable?

Variables are the reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory.


2 Answers

To do this with a hash:

Store:

$myHash{q3} = "ex. data 3";

Retrieve:

$result = $myHash{q3};

This has multiple benefits such as:

  • Satisfies "use strict";

  • You can loop over all the field names via keys %myHash

  • Since the field names are a list as per the last point, you can do any other list operations on them if needed (map, grep) etc...

    • For example, to get only the values where field name is of a form "q[1-5]", you can do:

      @subset = @myHash{ grep m/q[1-5]/ keys %myHash }; # Use a slice @{} operator.
      
    • Most database APIs (e.g. DBI) have calls which will automatically return back this exact format of a hash (or rather a hash reference instead) when querying a row from a table

      $hash_ref = $sth->fetchrow_hashref;
      
like image 149
DVK Avatar answered Nov 15 '22 00:11

DVK


You don't want to dynamically generate variable names! Instead, use a hash or an array:

my @q = ("ex. data 1", ..., "ex. data 5");

my $contents  = $q[ $some_index ];

print $contents;

Where $some_index is set to the desired index, thus removing any need for dynamic names.

like image 32
amon Avatar answered Nov 15 '22 00:11

amon