in reply to Re^3: recursive. again.
in thread recursive. again.

Sorry, I'm kind of a beginner, so I'm not really sure. What I should do is call the number function at  print $array[$i-1], right? Or should I do that before I push anything in there?

Replies are listed 'Best First'.
Re^5: recursive. again.
by muba (Priest) on Aug 24, 2010 at 01:59 UTC

    Think of a subroutine as a small script on its own. Like a script, you can write a subroutine, but if you want it to do something you'll have to explicitely run it. Merely writing a script isn't going to get you anywhere - you have to run your script. The same goes for subroutines - you can write one but if you never run it, nothing happens.

    Compare (and run) these two simple examples:

    # Having a cake... sub hello { print "Hello!\n"; }

    and

    # Having a cake... sub hello { print "Hello!\n"; } # And eating it, too! hello;

    The way you wrote your script, you are merely having the cake but you never eat it. The code inside the numbers subroute (which purpose it is to populate the array) is never executed, hence the array being empty at your final print statement.

Re^5: recursive. again.
by TomDLux (Vicar) on Aug 24, 2010 at 05:45 UTC

    Think of it like writing an email, but never pressing the SEND button.

    Or maybe it's more motivational if you imagine someone filling out a PayPal page to transfer a large sum of money to your account. They've defined the subroutine .. ie, filled in the page. You just need them to invoke the subroutine, ie press the SUBMIT button.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.