Gossamer Forum
Quote Reply
Number of Children
I notice Links already has functions for calcuation the number of children or parents a category has, I think I saw it in Build.pm from memory. I have used the parents function before sucessfully, but I cannot seem to get the children one to work for me.

Example:

my $ct = $DB->table('Category');
my $number_of_children = $ct->children($id);

Gives me: ARRAY(0x82a4264)

and if I try printing $ct->children($id);

I get: Links::Category=HASH(0x83278c0)->children(1)

Any ideas (probably something really basic to do with referencing the array?)Crazy


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Number of Children In reply to
The children method is in Links/Category.pm. It returns an arrayref of the kid's ids.

So $number_of_children is an arrayref (which would be more appropriately called $children). To see what's inside, do something like

print join ' ', @$children;

To access the elements of an arrayref, do $children->[3], instead of $array[3], as you would with an array.

To get the number of children, you could do

$number_of_children = @$children;

Recommended reading: http://www.perldoc.com/...6.1/pod/perlref.html

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Number of Children In reply to
Thanks Ivan! I was just about to hit the hay when you replied... so I have to try this out first!

Thanks for the reading reference too!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Number of Children In reply to
Just a mini addition to yogi's reply:

my $number_of_children = @$children;

....gives you the number of children (as does scalar @$children or ~~ @$children)....but:

my ($number_of_children) = @$children;

...will assign the first element to $number_of_children

Last edited by:

Paul: Jun 14, 2002, 1:32 AM
Quote Reply
Re: [Paul] Number of Children In reply to
Thanks very much Paul!

Between your post and yogi's post, I can't tell you home much has clicked in my head and now makes sense!!! Your explanations are so much clearer and to the point than what I can find on the net, or in my books here.

Dare I say it, I feel I have graduated Perl 101! Onto 102Wink.

Thank you both!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Number of Children In reply to
I forgot to explain why...oops...

In the first example, $number_of_childen is taken in scalar context and that is why you end up with the number of elements as the value. By adding the ( ) around the variable name it changes to list context which is why you end up with the first array element (an array is a list of values).

Last edited by:

Paul: Jun 14, 2002, 8:59 AM
Quote Reply
Re: [Paul] Number of Children In reply to
Now that makes sense too. Perl seems to be able to change a variable's "type" just by puting it in the sytax of the type. Very cleaver... gosh it will be hard to go back to ASP after this!

My little notepad notes on perl started out as a small file, you should see it now!


http://www.iuni.com/...tware/web/index.html
Links Plugins