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

Good. Now to the next problem. When I pass your program the value 5, I get a warning message on this line:
print $array[ $i - 1 ];

I'm pretty sure $i is initialized. What about the array? Let's check:

use Data::Dumper; print Dumper(\@array);

Nope. The array is empty. That's because you never call the number function, as AnonyMonk already pointed out.

Replies are listed 'Best First'.
Re^4: recursive. again.
by derpp (Acolyte) on Aug 24, 2010 at 00:36 UTC
    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?

      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.

      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.