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

Hey, monks.

I've decided to give up on my stock thing until later, when I don't have a massive headache from trying over and over again. So, I've moved on to a much simpler one to try. I've managed to mess that one up too. So, I'm trying to make the first number go up by 10%, second number go down by 5%, third up by 10%, fourth down by 5%, and so on. If you're wondering about the 1100 and 950, I've already multiplied 1.1% and 0.95% by 1000 to save lines. So yeah, my problem is that when I run it, nothing comes out. It's just blank. I've tried rewriting it, but to no avail. Maybe I'm just doing this totally wrong.

use warnings; use strict; my @array; print "Enter the number of years: "; my $i = <STDIN>; my @p = 100; sub number { for(1..100) { if ($_ % 2) { my $y = number($p[-1]*1100); push @array, $y; } else { my $z = number($p[-1]*950); push @array, $z; } } } print $array[$i-1];

Replies are listed 'Best First'.
Re: recursive. again.
by Anonymous Monk on Aug 24, 2010 at 00:03 UTC
    This is how its supposed to go
    ... print number( $i ); ... sub number { ... return "an actual number" }
Re: recursive. again.
by toolic (Bishop) on Aug 24, 2010 at 00:03 UTC
    When I download and try to run your code, I get a compile error on this line:
    push @array, yx;
      ah, oops. Fixed it, and tried to run again. Guess it wasn't the problem.
        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.