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

This seems like a simple question, but I just can't seem to make it work.
I want to prompt a user for a number and then start at 1 and create a string of numbers.
so say a user enters 5, the code will start at 1 and count 1,2,3,4,5.
#!/usr/bin/perl while() my $count=o; { print 'Enter a number'; chomp ($input = <STDIN>); foreach $input {$count++ } last; }

Replies are listed 'Best First'.
Re: Count from 1 after a user entered number
by Joost (Canon) on Mar 22, 2008 at 23:23 UTC
    First off, you should start your code with:
    use warnings; use strict;
    and possibly, if you want more explanations,
    use diagnostics;
    That would have caught at least a couple of errors in your code. Since this seems a LOT like homework, I'm only giving hints.

    o (the letter) isn't 0 (the number). use a better editor/font if you can't see the difference.

    You've got blocks { .. } where you don't need any.

    foreach $input isn't valid, and since $input isn't a list you're not gaining anything by looping over it.

    You probably want something like: foreach (1 .. $end) { ... } instead.

    last; unconditionally ends the loop. but there isn't any useful loop here.

    you're not print()ing anything.

      Probably appears like "homework" but it is a part of a larger script that I am piecing together. Thanks for your help.
Re: Count from 1 after a user entered number
by FunkyMonk (Bishop) on Mar 22, 2008 at 23:24 UTC
    Your specification is quite loose, so this may not be what you're after :(

    $| = 1; # autoflush print "Enter last number: "; chomp( my $last = <STDIN> ); print join( ", ", 1 .. $last ), "\n"; #Output: #1, 2, 3, 4, 5

    Update: Just in case you think I disagree with Joost, you do want

    use strict; use warnings;

    near the start of your script.

    Update^2: I've done no error checking (negative number, not a number, blank line etc), thanks ww. I'll leave that as an exercise. Oh, and chomp isn't needed either.

Re: Count from 1 after a user entered number
by ww (Archbishop) on Mar 23, 2008 at 01:08 UTC

    Using the range operator (1..$end) is the way to go, but another (less desireable) variation uses a C-style loop:

    #!/usr/bin/perl use strict; use warnings; my $output = "1 "; print "Enter a positive integer: "; my $lastnum = <STDIN>; # chomp $lastnum; # not needed here if ( $lastnum =~ /^[0-9]+$/ ) { # TEST, don't simply trust, use +r input! my $n = 2; while ( $n < ($lastnum+1) ) { $output .= $n; $output .= " "; $n++; } print "$output \n"; } else { print "Bad input\n"; exit; }

    Runs thus:

    ww@GIG:~/pl_test$ perl 675693.pl Enter a positive integer: -3 Bad input ww@GIG:~/pl_test$ perl 675693.pl Enter a positive integer: 27 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 +7 ww@GIG:~/pl_test$ perl 675693.pl Enter a positive integer: 1.35 Bad input ww@GIG:~/pl_test$
Re: Count from 1 after a user entered number
by oko1 (Deacon) on Mar 23, 2008 at 04:23 UTC

    Since nobody's mentioned it yet, you should

    use warnings; use strict;

    [grin]

    You should also validate the user's input. Never trust a user to do the right thing - even if you're the user. We all have our senior moments.

    { # For the sake of the example, this assumes that you # want to limit the possible input to this range print "Please enter a number [1-9999]: "; chomp($in = <STDIN>); redo unless $in =~ /^\d+$/ && $in > 0 && $in <= 9999; } print join(",", 1 .. $in), "\n";

    Last but not least, if you decide to not use the '..' range operator, you can do this in any number of silly ways:

    print $in--; do { print ", $a" } while --$a; for ($i=1;$i <= $in; $i++){ print $i-1?",$i":$i; } while($in){push @a, $in--;} print join ",", reverse @a;

    ...but you're better off sticking with the tried and true.