By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,278 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,278 IT Pros & Developers. It's quick & easy.

Accessing data in references?

P: 1
Let's say we have the following code:
Expand|Select|Wrap|Line Numbers
  1. my $VAR1 = {
  2.           'target' => {
  3.                         'oranges' => {
  4.                                             'size' => 'big'
  5.                                      },
  6.                         'apples' => {
  7.                                             'size' => 'small'
  8.                                     }
  9.                       }
  10. }
  11.  
  12. print $VAR1;
  13.  

Now, to get the size of apples, we would use:
Expand|Select|Wrap|Line Numbers
  1. print $VAR1->{'target'}->{'oranges'}->{'size'};
  2.  
But to do this, I have to know that there are oranges stored there, and that their size is defined.

How can I iterate all elements that are stored? I would like to display:

oranges, size: big
apples, colour: red
Mar 11 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
Let's say we have the following code:
Expand|Select|Wrap|Line Numbers
  1. my $VAR1 = {
  2.           'target' => {
  3.                         'oranges' => {
  4.                                             'size' => 'big'
  5.                                      },
  6.                         'apples' => {
  7.                                             'size' => 'small'
  8.                                     }
  9.                       }
  10. }
  11.  
  12. print $VAR1;
  13.  

Now, to get the size of apples, we would use:
Expand|Select|Wrap|Line Numbers
  1. print $VAR1->{'target'}->{'oranges'}->{'size'};
  2.  
But to do this, I have to know that there are oranges stored there, and that their size is defined.

How can I iterate all elements that are stored? I would like to display:

oranges, size: big
apples, colour: red
loops and references:

Expand|Select|Wrap|Line Numbers
  1. my $VAR1 = {
  2.           'target' => {
  3.                         'oranges' => {
  4.                                             'size' => 'big'
  5.                                      },
  6.                         'apples' => {
  7.                                             'size' => 'small'
  8.                                     }
  9.                       }
  10. };
  11.  
  12. foreach my $keys (keys %{$VAR1}) {
  13.     foreach my $sub_keys (keys %{$VAR1->{$keys}}) {
  14.         print "$sub_keys, ";
  15.         foreach my $sub_sub_keys (keys %{$VAR1->{$keys}->{$sub_keys}}) {
  16.             print "$sub_sub_keys: $VAR1->{$keys}->{$sub_keys}->{$sub_sub_keys}\n";
  17.         }
  18.     }
  19. }
  20.  
See the reply on devshed where a member there took a lot of time and effort to reply to your question.
Mar 11 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
If I may add, take a look at this web site as it may help with issues like this in the future.

Regards,

Jeff
Mar 11 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.