hash

Discussion in 'Perl Misc' started by mike, Dec 6, 2005.

  1. mike

    mike Guest

    If I am iterating over a set of data and I'd like to put that data into
    a hash.

    My data would be:

    fred 578 great
    fred 579 cool
    sue 765 super
    sue 766 yuppy
    sue 767 puppy

    As I iterate over that data loading it to the hash:

    while ( @row = $sth->fetchrow() )
    {
    $TV{$row[0]} = { uid=>$row[0], ids => [ id=>$row[1], title=>$row[2]]
    };
    }

    I think my hash then "should" look like:

    %TV =
    (
    "fred" =>
    {
    uid => "fred",
    ids => [
    { id => "578", title => "great", },
    { id => "579", title => "cool", },
    ],
    },
    "sue" =>
    {
    uid => "sue",
    ids => [
    { id => "765", title => "super", },
    { id => "766", title => "yuppy", },
    { id => "767", title => "puppy", },
    ],
    },
    );

    Then as I display that data:

    foreach my $key ( keys %TV )
    {
    print "$TV{$key}{uid} @{ $TV{$key}{ids} }\n";
    }


    Only bad thing is only the first uid gets loaded so I'm only getting:
    fred 578 great
    sue 765 super

    Any help is appreciated.

    Mike
     
    mike, Dec 6, 2005
    #1
    1. Advertising

  2. mike

    J. Gleixner Guest

    mike wrote:
    > If I am iterating over a set of data and I'd like to put that data into
    > a hash.


    Next time, a better subject would be more helpful.

    >
    > My data would be:
    >
    > fred 578 great
    > fred 579 cool
    > sue 765 super
    > sue 766 yuppy
    > sue 767 puppy
    >
    > As I iterate over that data loading it to the hash:
    >
    > while ( @row = $sth->fetchrow() )
    > {
    > $TV{$row[0]} = { uid=>$row[0], ids => [ id=>$row[1], title=>$row[2]]
    > };
    > }

    ....

    > Then as I display that data:
    >
    > foreach my $key ( keys %TV )
    > {
    > print "$TV{$key}{uid} @{ $TV{$key}{ids} }\n";
    > }
    >
    >
    > Only bad thing is only the first uid gets loaded so I'm only getting:
    > fred 578 great
    > sue 765 super
    >
    > Any help is appreciated.


    Since your key ('fred' and 'sue') isn't unique, the value for %TV{key}
    is over-written on each iteration.

    Try:

    while ( my @row = $sth->fetchrow() )
    {
    push( @{ $TV{$row[0]} }, { uid=>$row[0], ids => [ id=>$row[1],
    title=>$row[2]]};
    }

    Storing 'uid' seems a bit redundant and take a look at bind_column, to
    make your code more readable. It could look like:

    while( $sth->fetch )
    {
    push( @{ $TV{$uid} }, {
    ids => [ id=>$id, title=>$title ]
    });
    }

    Then you'll probably need an additional loop to go through each key that
    may have more than one entry:

    for my $uid ( keys %TV )
    {
    for my $href ( @{ $TV{ $uid } } )
    {
    ...
    }
    }

    See perldoc perldsc for more information on data structures.
     
    J. Gleixner, Dec 6, 2005
    #2
    1. Advertising

  3. mike

    mike Guest

    J,

    thank you very much.

    works like a charm.

    Mike
     
    mike, Dec 6, 2005
    #3
  4. mike

    mike Guest

    If I use:

    push( @{ $TV{$row[0]} }, { files => [ fileid=>$row[1], aims=>$row[2],
    filename=>$row[3] ] } );

    and then try to iterate over them like:

    for my $uid ( keys %TV)
    {
    print "$uid\n";
    for my $href ( @{ $TV{$uid} } )
    {
    #print "\t\t $href\n"; #a hash
    #print "\t\t $href->{files} \n"; #an array
    #print "\t\t @{ $href->{files} }\n"; #all array values
    print "\t\t @{ $href->{files} }{aims}\n"; #individual value
    }
    }

    But I can't seem to get my pointer correct on the individual value.
    What I'd like to do is capture the value of the fileid or the aims.

    I think I'm close, but no cigar.

    Mike
     
    mike, Dec 6, 2005
    #4
  5. Gunnar Hjalmarsson, Dec 7, 2005
    #5
  6. mike wrote:

    > push( @{ $TV{$row[0]} }, { files => [ fileid=>$row[1], aims=>$row[2],
    > filename=>$row[3] ] } );


    You have a typo in one of your anonymous HASH constructors there.

    You have put [...] (i.e. an anonymous ARRAY constructor) where you
    should have {...}.

    > print "\t\t @{ $href->{files} }{aims}\n"; #individual value


    A single element slice is more simply written without the slice.

    print "\t\t $href->{files}{aims}\n";
     
    Brian McCauley, Dec 7, 2005
    #6
  7. mike

    mike Guest

    > So, first you insult most of those able to help:
    > http://groups.google.com/group/comp.lang.perl.misc/msg/17dd1caa3edebb10


    Not insulting anyone merely relaying my experience with this group.
    I've read stuff from this group now for years and it is probably the
    worst in terms of actually providing help. Too much griping (crying)
    back to posters on their asking for help. I think some of them just go
    away and decide to use another language is they can't figure it out.

    You know more and more businesses are trying to get away from Perl
    programming altogether and the constant crying to posters does not help
    develop any skills or build up the Perl group or expertise.
     
    mike, Dec 7, 2005
    #7
  8. mike

    mike Guest

    Brian, thank you very much for pointing me in the correct direction. I
    was looking at trying to display the items in the structure instead of
    a problem creating the structure.

    Again, good to get an experts review.
     
    mike, Dec 7, 2005
    #8
    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. Red Orchid
    Replies:
    3
    Views:
    1,054
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,122
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    793
  4. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    629
    David A. Black
    Jul 2, 2008
Loading...

Share This Page