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

Sounds stupid but I want to be able to run a loop that will add 1 to a set variable until it reaches another set variable and put that into a array.
#!/usr/bin/perl -w use strict; use warnings; print "Enter first Number: "; my $ipa = <STDIN>; print "Enter last Number: "; my $ipb = <STDIN>; my $ipc = $ipa +1; while ($ipc <= $ipb) { $ipc; print $ipc; } # my @range = ($ipa, $ipb); if ($ipc < $ipb){ @range = ($ipa, $ipb); unshift(@range, $ipc); } else { print $ipc; } sub ascend { $ipa = $ipb; } my @rl = sort ascend @range; print join(",",@rl);

Replies are listed 'Best First'.
Re: Adding Numbers
by GrandFather (Saint) on Nov 21, 2010 at 04:18 UTC

    Sounds stupid, but I can't guess what you have tried without you show me the code and I can't guess where you are having trouble without you either tell me or show me what you've tried.

    Generally we give better answers if you show us some code, show us a small sample of input data (if appropriate), expected output and what you actually get. Make sure your sample code is small, runs as you describe and that you use strictures (use strict; use warnings;).

    Update: Oh, and please indicate when you update your node.

    True laziness is hard work
Re: Adding Numbers
by kcott (Archbishop) on Nov 21, 2010 at 05:13 UTC

    If you'd actually run this code, you would have seen:

    Useless use of private variable in void context at <scriptname> line 1 +5.

    Perl is telling you what's wrong and where the problem is. Perhaps you could at least make some minimal effort before posting here. There's only 5 characters on line 15 - hardly a major debugging task.

    If you didn't understand the message, then that should have been your question. And, if that is the case, add use diagnostics; to the top of your code to get more verbose messages.

    Please read: How do I post a question effectively?

    -- Ken

      That still hasn't answer my question. Whilst i've read the diagnostics I still dont understand why I can't list a range of numbers from variable a to variable b.

        I am still of the opinion that you have not run this code.

        So, try running the code you have shown here and post the prompts, the first and last values you enter and the output including any error messages.

        Of course, if you do actually run it, the problem might become obvious.

        -- Ken

Re: Adding Numbers
by TomDLux (Vicar) on Nov 21, 2010 at 05:59 UTC

    When I run your program, and enter the numbers '3' and '7', the output I get is:

    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    44444444444444444444444444444444444444444444444444444444444444444444444444444444
    

    At that point I pressed Control-C to interupt the program.

    Is that what you got? Is that the result you were expecting? In what way did my experience differ from your expectations?

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      the output i'm looking for, given '3' and '7' is 3,4,5,6,7
        my @range = 3 .. 7;
        True laziness is hard work

        You want the result to be '3 4 5 6 7' and the actual result is '4 4 4 4 4 ...'. So there's two things wrong, the first is that you start at '$ipa + 1' rather than at '$ipa'. What's the second thing that's wrong?

        As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Adding Numbers
by PeterPeiGuo (Hermit) on Nov 21, 2010 at 05:58 UTC

    Look up perl range operator ..

    use Data::Dumper; use strict; use warnings; print "Enter first Number: "; my $ipa = <STDIN>; print "Enter last Number: "; my $ipb = <STDIN>; my @foo = ($ipa..$ipb); print Dumper(@foo);

    Peter (Guo) Pei

Re: Adding Numbers
by Anonymous Monk on Nov 21, 2010 at 10:42 UTC
    What exactly do you expect to happen here:
    while ($ipc <= $ipb) { $ipc; print $ipc; }
    The variable $ipc isn't going to change.
Re: Adding Numbers
by Anonymous Monk on Nov 21, 2010 at 05:56 UTC
    Hi,

    Try looking at 'for' and 'push'.

    J.C.