Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > accessing a hash

Reply
Thread Tools

accessing a hash

 
 
Michael Hill
Guest
Posts: n/a
 
      03-04-2004
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
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      03-04-2004
"Michael Hill" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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



 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-04-2004

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
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      03-04-2004
Michael Hill <(E-Mail Removed)> 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
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      03-05-2004
"David K. Wall" <(E-Mail Removed)> wrote:

> Michael Hill <(E-Mail Removed)> 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 ..."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Accessing a hash in a hash Justin To Ruby 3 06-05-2008 07:17 PM
accessing hash values with %hash->{foo} ? Paul Lalli Perl Misc 2 05-31-2006 04:50 PM
Accessing Hash of hash of arrays Lynn Perl Misc 9 02-16-2005 06:59 PM



Advertisments