Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Symbolic references

by Anonymous Monk
on Apr 15, 2013 at 04:18 UTC ( #1028671=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to take in an argument like var1/var2/var3 and transform it to a symbolic variable I can use $ret->{var1}->{var2}->{var3} to access some data.
foreach $argnum (0..$#ARGV){ print "$ARGV[$argnum]\n"; my $xpath=$ARGV[$argnum]; $xpath =~ s/\//}->{/g; eval ($xpath = "{" . $xpath . "}"); #xpath variable should becode like: $ret->{link}->{rel} print ${$ret->$xpath}; }
However, I keep getting "Can't call method {} on unblessed reference"

I've also read that symbolic references are bad, so is there a better way to accomplish what I'm trying to do?

Comment on Symbolic references
Download Code
Re: Symbolic references
by LanX (Canon) on Apr 15, 2013 at 04:35 UTC
    > so is there a better way to accomplish what I'm trying to do?

    DB<100> $path='var1/var2/var3' # input => "var1/var2/var3" DB<102> @path=split /\//,$path # extract path => ("var1", "var2", "var3") DB<103> $hash_ref->{var1}{var2}{var3}=42 # init nested test-hash => 42 DB<104> $node=$hash_ref # set start => { var1 => { var2 => { var3 => 42 } } } DB<105> $node=$node->{$_} for @path # walk down path => "" DB<106> $node # check result => 42

    HTH! =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      You've partially lost me with that answer.

      What if I may have a variable number of variables..
      program.pl var1 program.pl var1/var2 program.pl var1/var2/var3
      etc? Is there a programatic way to read N variables into the reference?
        I've changed my code to be less terse and hopefully better understandable step by step.

        > What if I may have a variable number of variables..

        you can put a unlimited anount of elements into '$path' and it will work as long as the intermediate values really exist within the nested hashes (otherwise the result is undef).

        > Is there a programatic way to read N variables into the reference?

        ??? what do you mean here with "reference"?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Symbolic references (diver)
by tye (Cardinal) on Apr 15, 2013 at 05:34 UTC
Re: Symbolic references
by kcott (Abbot) on Apr 15, 2013 at 12:41 UTC

    As you're reading from the commandline, you'll probably want either a way to warn of invalid keys or, perhaps, ignore them. The following does the latter.

    A valid list of keys:

    $ perl -Mstrict -Mwarnings -E ' my %x = ( a => { b => { d => 4, e => 5 }, c => 3 } ); my $y = \%x; for (split /\// => $ARGV[0]) { last unless ref $y; $y = $y->{$_}; } say $y if defined $y; ' a/b/e 5

    A partially valid list of keys:

    $ perl -Mstrict -Mwarnings -E ' my %x = ( a => { b => { d => 4, e => 5 }, c => 3 } ); my $y = \%x; for (split /\// => $ARGV[0]) { last unless ref $y; $y = $y->{$_}; } say $y if defined $y; ' a/c/e 3

    An invalid list of keys:

    $ perl -Mstrict -Mwarnings -E ' my %x = ( a => { b => { d => 4, e => 5 }, c => 3 } ); my $y = \%x; for (split /\// => $ARGV[0]) { last unless ref $y; $y = $y->{$_}; } say $y if defined $y; ' a/d/e

    -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2015-07-05 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (66 votes), past polls