Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell the size of an anonymous array of hash? 1

Status
Not open for further replies.

cyan01

Programmer
Mar 13, 2002
143
0
0
US
A few days ago, I asked how to tell the size of a multidimensional array's size. Thank you all for your attention and kind help on that one!

Now, I am having a similar question. I have an anonymous array of hash below and a for loop to display it's elements:

-----------------------------------

$foo = [
[
{ name => 'Dan',
id => '0001',
},
{ name => 'Bob',
id => '0002',
},
],
[
{ name => 'Tom',
id => '0003',
},
],
];

for($i=0; $i<2; $i++) # boundary is hard coded
{
for($j=0; $j<2; $j++) # boundary is hard coded
{
print "\$i = $i, \$j = $j, \$foo->[$i][$j]->{name} = $foo->[$i][$j]->{name}";
print ", \$foo->[$i][$j]->{id} = $foo->[$i][$j]->{id}\n";
}
}
----------------------------------------

here is the output:

$i = 0, $j = 0, $foo->[0][0]->{name} = Dan, $foo->[0][0]->{id} = 0001
$i = 0, $j = 1, $foo->[0][1]->{name} = Bob, $foo->[0][1]->{id} = 0002
$i = 1, $j = 0, $foo->[1][0]->{name} = Tom, $foo->[1][0]->{id} = 0003
$i = 1, $j = 1, $foo->[1][1]->{name} = , $foo->[1][1]->{id} =

Note that the last line is not necessary. Could someone please tell me how to determine the boundary w/o hard coding? Many thanks!

 
What about this:

Code:
$foo = [
         [
           { name => 'Dan',
             id => '0001',
           },
           { name => 'Bob',
             id => '0002',
           },
         ],
         [
           { name => 'Tom',
             id => '0003',
           },
         ],
       ];

$i=0;
while (defined $foo->[$i]) {
  $j=0;
  while (defined $foo->[$i][$j]) {
    print "\$i = $i, \$j = $j, \$foo->[$i][$j]->{name} = $foo->[$i][$j]->{name}";
    print ", \$foo->[$i][$j]->{id} = $foo->[$i][$j]->{id}\n";
    $j++;
  }
  $i++;
}


--G
 
Thank you. I just tested on solaris 8 and the output is exactly the same. :(
 
I intended to output the same, only without having to know ahead of time what the array dimensions were.

Perhaps I misunderstood your goal. (I'm a little tired today, sorry.) I thought the problem was how to do this without hard-coding the array boundaries. Could you restate the question?

--G
 
The last line in the output is NOT necessary:

$i = 1, $j = 1, $foo->[1][1]->{name} = , $foo->[1][1]->{id} =

I know why my code is wrong. But I would think your is right. I don't understand why your code generates the same output?
 
OH. I see. No, i misspoke slightly - the output I get is exactly the same as yours, except mine doesn't generate the last line you are not wanting. Here is what I get, on Solaris 8, with Perl 5.8, using the code as I posted it:
Code:
$i = 0, $j = 0, $foo->[0][0]->{name} = Dan, $foo->[0][0]->{id} = 0001
$i = 0, $j = 1, $foo->[0][1]->{name} = Bob, $foo->[0][1]->{id} = 0002
$i = 1, $j = 0, $foo->[1][0]->{name} = Tom, $foo->[1][0]->{id} = 0003

If you still get the 4th line from your sample output, something else must be amiss. What version of Perl are you using?

--G
 
You are right! My mistake! Thank you.
 
Oh good! Glad that helped.

Incidentally, if you want to actually know what the first level dimension is, this will get it:

$first_dim = $#{$foo};

After that you could use a similar syntax to find out the second dimension of each first dimension:

$second_dim_0 = $#{$foo->[0]};

Cheers!

--G
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top