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 | |
by kennethk (Abbot) on Jun 28, 2012 at 13:58 UTC | |
|
Re: Using Variable Value as Variable Name
by locked_user sundialsvc4 (Abbot) on Jun 28, 2012 at 14:02 UTC | |
|
Re: Using Variable Value as Variable Name
by Anonymous Monk on Jun 28, 2012 at 14:03 UTC | |
by umama (Initiate) on Jun 28, 2012 at 14:21 UTC | |
by umama (Initiate) on Jun 28, 2012 at 14:25 UTC | |
by kennethk (Abbot) on Jun 28, 2012 at 14:41 UTC | |
by Anonymous Monk on Jun 28, 2012 at 14:04 UTC | |
|
Re: Using Variable Value as Variable Name
by Anonymous Monk on Jun 28, 2012 at 13:30 UTC | |
by umama (Initiate) on Jun 28, 2012 at 13:35 UTC |