Statue has asked for the wisdom of the Perl Monks concerning the following question:
Hello Perl Monks:
I was wondering if there was a way to do this (you will get the idea when you see the code - if not, I'll explain below):
#!/usr/bin/perl -w use strict "vars" ; my $data ; my @splitter ; $data = "testval,1" ; @splitter = split( "," , $data ) ; my $$splitter[ 0 ] = $splitter[ 1 ] ;
I know you can do something like this with hashes, but I don't want to use hashes. Here's the gist of what I am trying to do with this code. I have data (defined here as $data, but will be in a file). It is comma-separated. The first value in this data is the actual variable name I want to define. The second value is the value I want to assign to it. Obviously, assigning the value is easy, but creating the variable is not so easy. The $$ makes sense in terms of real-time substitution, however it doesn't work. Perl is interpreting it to mean something other than a string declaration. The error message given is:
Can't declare scalar dereference in "my" at srtest line 7, near "$spli +tter[" syntax error at srtest line 7, near "$splitter[" srtest had compilation errors.
Any ideas of how to make this work?
Statue
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Self-writing code revisited
by kennethk (Abbot) on Sep 05, 2013 at 23:44 UTC | |
|
Re: Self-writing code revisited
by kcott (Archbishop) on Sep 06, 2013 at 01:02 UTC |