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

Hi again gang
Similar question to my last one. If I have:
$api_name[0] = 'get_country()'; $var_name[0] = 'Country';
And I want to do this sort of thing on a google GA query
for my $row ( @{ $res->rows } ) { $Country = $row->get_country(); $sql = "insert into table set country = '$Country'... etc
But I can't hard code the variable names or the api name as every query is different, so I want to:
$[$var_name[0]] = $row->$[$api_name[0]];
Any thoughts on how I'd get that last line to work?

Replies are listed 'Best First'.
Re: Supplementary Google GA question
by huck (Prior) on Sep 12, 2017 at 02:24 UTC

    I think you are going in the whole wrong direction. Coordinated arrays is not a good idea for starters. See if you can follow how/why this works and go from there

    use strict; use warnings; my @sets; push @sets,{name=>'Country',api=>'get_country()'}; push @sets,{name=>'State' ,api=>'get_state()'}; push @sets,{name=>'fake' ,api=>'get_fake()'}; my %subs; $subs{'get_country()'}=\&get_country; $subs{'get_state()'} =\&get_state; for my $set (@sets) { my $vname=$set->{name}; my $api =$set->{api}; my $call=$subs{$api}; my $res; if (defined $call) {$res=&$call;} $set->{res}=$res; } use Data::Dumper; print Dumper (\@sets); exit; sub get_country{ return 'USA'; } sub get_state{ return 'FL'; }

      I think you're right!
      Got something working now along the lines of what you proposed. Thanks so much.