o_0 has asked for the wisdom of the Perl Monks concerning the following question:

I got some code from the Perl Cookbook that generates a random tree and prints it out 3 different ways. I changed it a little, so that the number of vertices would be random. I want the code to count the number of vertices. Some output is below. It says the number of vertices is 1, but how do I get it to count the 5 vertices?
Sample output:
Pre order: 760 75 455 751 839 In order: 75 455 751 760 839 Post order: 751 455 75 839 760 Number of vertices: 1
Code:
#!/usr/bin/perl -w use strict; my($root, $n); # first generate random inserts while ($n++ < int(rand(5))+5) { insert($root, int(rand(1000)))} # now dump out the tree all three ways print "Pre order: "; pre_order($root); print "\n"; print "In order: "; in_order($root); print "\n"; print "Post order: "; post_order($root); print "\n"; vertices($root); exit; ################### sub vertices { my $number=@_; print "Number of vertices: $number"; }

Replies are listed 'Best First'.
Re: program that counts all the vertices in a given tree
by roboticus (Chancellor) on Jul 01, 2013 at 01:28 UTC

    o_O:

    Your vertices function is printing the number of arguments you gave it: The @_ variable is the list of arguments passed to your subroutine. Since you pass it $root, you'll always get 1 for the result.

    You either need to expand the tree into the list of vertices and pass that list to the vertices subroutine, or have the vertices subroutine traverse the tree and keep count.

    You haven't shown the code for the XX_order subroutines, but based on the rest of your code, you could simply replace the print statement with an increment to get the vertex count.

    Show a bit more of your code and we can give you a little more help.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      The rest of the program is given in the link above. I don't see a way to delete that.
      I tried doing this instead:
      vertices(pre_order($root))
      but that just gives the output of pre_order($root) again and says the number of vertices is 0.
        "traverse the tree and keep count" - that was the ticket, thanks. By the way, someone mentioned plagiarism. Is it plagiarism if I mentioned the book, and also I modified the code?
Re: program that counts all the vertices in a given tree
by Anonymous Monk on Jul 01, 2013 at 01:21 UTC

    What tree where?

    $ perl fudge Undefined subroutine &main::insert called at fudge line 7.

        Not a really good answer.

        1. Your link may or may not be live at some time in the future... some time, for example, when some future reader tries to understand what you were doing.
        2. Regardless of the discussion below and elsewhere re copyright, pirate sites, etc., linking to an even-questionably-illegal copy of a copyrighted work is -- in my not-at-all humble opinion -- unethical.

        If you didn't program your executable by toggling in binary, it wasn't really programming!

        This is a link to an illegal copy, you better delete it again by yourself or the local admins will!

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: program that counts all the vertices in a given tree
by LanX (Saint) on Jul 01, 2013 at 01:28 UTC
    ♪..♫ Nobody knows the data you use, nobody knows but Jesus ...♩..♬

    Cheers Rolf

    ( addicted to the Perl Programming Language)