hash

M

mike

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
 
J

J. Gleixner

mike said:
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.
 
M

mike

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
 
B

Brian McCauley

mike said:
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";
 
M

mike

So, first you insult most of those able to help:

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.
 
M

mike

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top