Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Accessing references

by diparun (Initiate)
on Apr 22, 2004 at 00:00 UTC ( #347172=perlquestion: print w/ replies, xml ) Need Help??
diparun has asked for the wisdom of the Perl Monks concerning the following question:

In this code output, how do I access the sub-components of column_defs, for eg : how do i extract the data_length of a in column_defs ? output :
create table as1( a float, b integer, c char) $VAR1 = { 'org_table_names' => [ 'as1' ], 'column_names' => [ 'A', 'B', 'C' ], 'command' => 'CREATE', 'table_names' => [ 'AS1' ], 'org_col_names' => [ 'a', 'b', 'c' ], 'column_defs' => { 'a' => { 'data_length' => undef, 'data_type' => 'FLOAT' }, 'b' => { 'data_length' => undef, 'data_type' => 'INTEGER' }, 'c' => { 'data_length' => undef, 'data_type' => 'CHAR' } } };

janitored by ybiC: Balanced <code> tags around codeblock, as per monastery convention

Comment on Accessing references
Download Code
Re: Accessing references
by borisz (Canon) on Apr 22, 2004 at 00:07 UTC
    my $data_length = $VAR1->{column_defs}->{a}->{data_length};
    Boris
Re: Accessing references
by asdfgroup (Beadle) on Apr 22, 2004 at 00:29 UTC
    or a bit shorter $VAR1->{column_def}{a}{data_length}
Re: Accessing references
by bart (Canon) on Apr 22, 2004 at 01:21 UTC
    Both answers given thus far are equivalent, the arrows between subindexes are optional — but not the arrow between the variable name and the first index! That arrow makes the distinction between a scalar, that is a hash/array ref (which needs the arrow), and a an actual array/hash variable.

    Anyway, please do take a look at the excellent tutorial for references that on modern perls is part of the core distro, as well as online: perlreftut

      But, how do I access them through code.?.. meaning, dynamically..

      diparun

        If you want to access the hash associated with column_defs, you might do, e.g. keys %{$VAR1->{'column_defs'}};
        That is, wrap the hash reference in curlies and then treat it exactly as you would a hash variable name. Is that what you're asking?

        Have you been through perldoc perlreftut?


        The PerlMonk tr/// Advocate

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (17)
As of 2014-08-28 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (264 votes), past polls