Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Using Variable Value as Variable Name

by umama (Initiate)
on Jun 28, 2012 at 13:24 UTC ( [id://978921]=perlquestion: print w/replies, xml ) Need Help??

umama has asked for the wisdom of the Perl Monks concerning the following question:

I've searched high and low, and I still cannot get this to work. Any suggestions are welcome! TIA

#!/usr/local/bin/perl use strict; use warnings; no strict "subs"; no strict "refs"; my $line = 'fields: name addr zip state city'; my $var_city; print "$line\n"; $line =~ s/[-)(]//g; my @columnNames = split(/\s+/, substr($line,9)); print @columnNames; print "\n\n"; my $acount = 0; foreach my $name (@columnNames) { $acount++; print "name is: $name\n"; ${var_."$name"} = $acount; #print "nname is: ${"$name"}\n" } print "this should display 5: $var_city\n";

Here is the output:

fields: name addr zip state city nameaddrzipstatecity name is: name name is: addr name is: zip name is: state name is: city Use of uninitialized value $var_city in concatenation (.) or string at + G:\Projects\WebLogReporting\VarValAsVarName.pl line 28. this should display 5:

Replies are listed 'Best First'.
Re: Using Variable Value as Variable Name
by toolic (Bishop) on Jun 28, 2012 at 13:38 UTC
Re: Using Variable Value as Variable Name
by sundialsvc4 (Abbot) on Jun 28, 2012 at 14:02 UTC

    Let me strongly echo what I am sure is a pretty-universal sentiment here:   don’t expose the variable-name space, even if you find that you can.   This creates a program that is not only un-debuggable, but also wantonly open to sabotage.   (And, yeah, you have to think about things like “sabotage.”)

    If you want to open access to specific variables’ values by name, here is one safe way to do so:   create a hash, whose keys are the variable-names, and whose contents for each key is a reference to that variable.   This is a controllable and debuggable situation, because “not just any ol’ name you might come up with” can be used; only what exists().   Now, presto!, comes the first obvious simplification:   why use a variable-name when you can simply refer to the hash?

    Even though we like to say “TMTOWTDI™ == There’s More Than One Way To Do It,” there are definitely ways that are recommend vs. others that are not...

Re: Using Variable Value as Variable Name
by Anonymous Monk on Jun 28, 2012 at 14:03 UTC

      Thanks to all for the info. I'm an new to Perl, and was able to do this in PHP. I'm new to PHP as well. I'm an old Cobol/SAS programmer, and I think I have a long way to go. :-)

      Additional info... my reason for doing this is that I'm processing text files for loading to a database and the column order can vary by file.
        A hash is a much more natural way of dealing with this, and IMHO one of Perl's greatest strengths (the other being regular expressions). You could even use lock_keys from Hash::Util to protect yourself from typos.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Boy, either I am a slow typer, or the responses come really quick here. Didn't mean to post a duplicate link.

Re: Using Variable Value as Variable Name
by Anonymous Monk on Jun 28, 2012 at 13:30 UTC

    You really ought to search before you post. This has been asked MANY times.

    The answer is: Why would you want to do that?!?

    Most likely you want a hash, sometimes its a reference to a variable, sometimes it is a more complex data structure.

      I have searched. Thanks so much for your friendly help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://978921]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-19 11:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found