accessing a hash

Discussion in 'Perl Misc' started by Michael Hill, Mar 4, 2004.

  1. Michael Hill

    Michael Hill Guest

    I have this hash I'm trying to traverse and having some trouble.

    my $var1 = {
    'database' => {
    'table' => {
    'fields' => {
    'field_name'
    => [

    'A',

    'B',

    'C',

    'D'

    ]
    },
    'name' => 'name of table'
    }
    }
    };

    This works:

    print "$var1->{database}->{table}->{name}\n\n";

    print "$var1->{database}->{table}->{fields}->{field_name}->[0]\n";
    print "$var1->{database}->{table}->{fields}->{field_name}->[1]\n";
    print "$var1->{database}->{table}->{fields}->{field_name}->[2]\n";

    Now if I want to loop through all those in:
    $var1->{database}->{table}->{fields}->{field_name} ?

    I'm attempting to do this pointing directly to an array and loop through
    the values but the error message says I am pointing to an element. How
    do I point to the elements array?

    foreach my $y ( keys %$var1->{database}->{table}->{fields}->{field_name}
    )
    {
    print "$y\n";
    }

    Now if I force nested elements to be arrays .... like:

    $var2 = {
    'database' => [
    {
    'table' => [
    {
    'fields' => [
    {

    'field_name' => [

    'A',

    'B',

    'C',

    'D'

    ]
    }
    ],
    'name' => [
    'name of
    table'
    ]
    }
    ]
    }
    ]
    };

    This doesn't work anymore:

    print "$var2->{database}->{table}->{name}\n\n";

    print "$var2->{database}->{table}->{fields}->{field_name}->[0]\n";

    Any how would I loop though the
    $var2->{database}->{table}->{fields}->{field_name} elements array?

    Thanks,

    Mike
     
    Michael Hill, Mar 4, 2004
    #1
    1. Advertising

  2. Michael Hill

    gnari Guest

    "Michael Hill" <> wrote in message
    news:...
    >
    > Now if I force nested elements to be arrays .... like:
    >
    > $var2 = {
    > 'database' => [
    > {
    > 'table' => [
    > {
    > 'fields' => [
    > {
    >
    > 'field_name' => [
    >
    > 'A',
    >

    ......


    > This doesn't work anymore:
    >
    > print "$var2->{database}->{table}->{name}\n\n";

    print "$var2->{database}[0]{table}[0]{fields}[0]{field_name}[0]\n\n";

    should print 'A'

    gnari
     
    gnari, Mar 4, 2004
    #2
    1. Advertising

  3. Michael Hill

    Paul Lalli Guest

    On Thu, 4 Mar 2004, Michael Hill wrote:

    > I have this hash I'm trying to traverse and having some trouble.
    >
    > my $var1 = {
    > 'database' => {
    > 'table' => {
    > 'fields' => {
    > 'field_name'
    > => [
    >
    > 'A',
    >
    > 'B',
    >
    > 'C',
    >
    > 'D'
    >
    > ]
    > },
    > 'name' => 'name of table'
    > }
    > }
    > };
    >
    > This works:
    >
    > print "$var1->{database}->{table}->{name}\n\n";
    >
    > print "$var1->{database}->{table}->{fields}->{field_name}->[0]\n";
    > print "$var1->{database}->{table}->{fields}->{field_name}->[1]\n";
    > print "$var1->{database}->{table}->{fields}->{field_name}->[2]\n";
    >
    > Now if I want to loop through all those in:
    > $var1->{database}->{table}->{fields}->{field_name} ?
    >
    > I'm attempting to do this pointing directly to an array and loop through
    > the values but the error message says I am pointing to an element. How
    > do I point to the elements array?
    >
    > foreach my $y ( keys %$var1->{database}->{table}->{fields}->{field_name}
    > )
    > {
    > print "$y\n";
    > }
    >

    Your syntax is a bit off.

    $var1->{database}->{table}->{fields}->{field_name} is an array reference.
    Therefore:
    @{$var1->{database}->{table}->{fields}->{field_name} is the array.


    So you should have
    foreach my $y (@{$var1->{database}->{table}->{fields}->{field_name}) {
    print "$y\n";
    }

    Paul Lalli
     
    Paul Lalli, Mar 4, 2004
    #3
  4. Michael Hill <> wrote:

    > I have this hash I'm trying to traverse and having some trouble.
    >

    [cleaned up for readability]

    my $var1 = {
    'database' => {
    'table' => {
    'fields' => {
    'field_name' => ['A','B','C','D']
    },
    'name' => 'name of table'
    }
    }
    };

    > This works:
    >
    > print "$var1->{database}->{table}->{name}\n\n";
    >
    > print "$var1->{database}->{table}->{fields}->{field_name}->[0]\n";
    > print "$var1->{database}->{table}->{fields}->{field_name}->[1]\n";
    > print "$var1->{database}->{table}->{fields}->{field_name}->[2]\n";



    A little syntactic sugar:

    print "$var1->{database}{table}{name}\n\n";

    print "$var1->{database}{table}{fields}{field_name}[0]\n";
    print "$var1->{database}{table}{fields}{field_name}[1]\n";
    print "$var1->{database}{table}{fields}{field_name}[2]\n";

    will work, and it's less typing.

    The arrow is optional when it is between bracket subscripts.

    > Now if I want to loop through all those in:
    > $var1->{database}->{table}->{fields}->{field_name} ?


    Tell perl it's (a reference to) an array by putting it inside @{}

    for my $element ( @{ $var1->{database}{table}{fields}{field_name} } ) {
    # do something with $element
    }

    >
    > Now if I force nested elements to be arrays .... like:


    Um... this code just puts anomymous hashes inside anonymous arrays.

    >
    > $var2 = {
    > 'database' => [
    > {
    > 'table' => [
    > {
    > 'fields' => [
    > {
    >
    > 'field_name' => [
    >
    >
    > 'A',
    >
    >
    > 'B',
    >
    >
    > 'C',
    >
    >
    > 'D'
    >
    >
    > ]
    > }
    > ],
    > 'name' => [
    > 'name of
    > table'
    > ]
    > }
    > ]
    > }
    > ]
    > };


    Cleaned up a little:

    $var2 = {
    'database' => [{
    'table' => [{
    'fields' => [{
    'field_name' => ['A','B','C','D']
    }],
    'name' => ['name of table']
    }]
    }]
    };

    Yeesh. I don't know what you were trying to do, but I very much doubt that
    this code does it. :)


    > This doesn't work anymore:
    >
    > print "$var2->{database}->{table}->{name}\n\n";
    >
    > print "$var2->{database}->{table}->{fields}->{field_name}->[0]\n";


    That's right, it doesn't, because $var is a reference to a different sort
    of data structure than that referenced by $var1.


    > Any how would I loop though the
    > $var2->{database}->{table}->{fields}->{field_name} elements array?


    There is no such thing as
    $var2->{database}->{table}->{fields}->{field_name} so there's no way to
    loop through it.

    --
    David Wall
     
    David K. Wall, Mar 4, 2004
    #4
  5. "David K. Wall" <> wrote:

    > Michael Hill <> wrote:

    [snip]
    >> This doesn't work anymore:
    >>
    >> print "$var2->{database}->{table}->{name}\n\n";
    >>
    >> print "$var2->{database}->{table}->{fields}->{field_name}->[0]\n";

    >
    > That's right, it doesn't, because $var is a reference to a different sort
    > of data structure than that referenced by $var1.


    Oops, make that "$var2 is a ..."
     
    David K. Wall, Mar 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    555
    red floyd
    Nov 10, 2011
  2. Justin To

    Accessing a hash in a hash

    Justin To, Jun 5, 2008, in forum: Ruby
    Replies:
    3
    Views:
    112
    Rodrigo Bermejo
    Jun 5, 2008
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    640
    David A. Black
    Jul 2, 2008
  4. Lynn

    Accessing Hash of hash of arrays

    Lynn, Feb 14, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    558
    Alan J. Flavell
    Feb 16, 2005
  5. Paul Lalli

    accessing hash values with %hash->{foo} ?

    Paul Lalli, May 31, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    109
    Brian McCauley
    May 31, 2006
Loading...

Share This Page