James Marks wrote:
>
> Down, near the bottom of this example code (marked), I'm trying to count
> the number of elements of an array which is the value of a key in a hash
> which is, itself, an element of an array. So far, I've been unsuccessful
> and I'm stumped as to what to try next.
>
> Help?
>
> my @web_sites_directory_tree  =
>
> (
>     {
>         title        => 'Web Site One',
>         directory    => 'web_site_one',
>         subdirectory => [
>                             'subdirectory_1',
>                             'subdirectory_2',
>                             'subdirectory_3',
>                             'subdirectory_4',
>                             'subdirectory_5',
>                             'subdirectory_6'
>                         ]
>     },
>     {
>         title        => 'Web Site Two',
>         directory    => 'web_site_two',
>         subdirectory => [
>                             'subdirectory_1',
>                             'subdirectory_2',
>                             'subdirectory_3'
>                         ]
>     },
>     etc...
> )
>
> # COUNT NUMBER OF DIRECTORIES - (THIS WORKS)
> $directory_count = @web_sites_directory_tree;
>
> for (my $i = 0; $i < $directory_count; $i++) {
>
>     my $title     = "$web_sites_directory_tree[$i]{title}";
>     my $directory = "$web_sites_directory_tree[$i]{directory}";
>
>     # COUNT NUMBER OF SUBDIRECTORIES IN DIRECTORY $i - (THIS DOESN'T WORK)
>     $subdirectory_count = $web_sites_directory_tree[$i]{subdirectory};
>
>     # (CONSEQUENTLY, THE REST OF THIS FAILS)
>     for (my $i = 0; $j < $subdirectory_count; $i++) {
>
>         my $subdirectory = $web_sites_directory_tree[$i]{subdirectory}[$j];
>
>         print "$title\n";
>         print "$directory\n";
>         print "$subdirectory\n";
>
>     }
>
> }
>
> Thanks for any help you can provide.

Hi James

Apart from what Dermot was able to help you with, you'd be better off with a few
tweaks:

- Don't put quotes around scalar values, ie:

  my $title = $web_sites_directory_tree[$i]{title};
  my $directory = $web_sites_directory_tree[$i]{directory};

This often works, but is almost always unnnecessary. And it will fail altogether
if you are copying references (as you may have found out when trying to copy the
{subdirectory} element?)

- Avoid the C-style for loop construct. There's almost certainly a better way to
do it.

- Grab references to sub-elements out of the main structure to avoid repeating
the full key/index list each time.

Take a look at this. Doesn't it look better?


  for my $i (0 .. $#web_sites_directory_tree) {

      my $title = $web_sites_directory_tree[$i]{title};
      my $directory = $web_sites_directory_tree[$i]{directory};
      my $subdir_list = $web_sites_directory_tree[$i]{subdirectory};

      for my $j (0 .. $#$subdir_list) {

          my $subdirectory = $subdir_list->[$j];

          print "$title\n";
          print "$directory\n";
          print "$subdirectory\n";
      }
  }


Cheers,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to