in reply to Count from 1 after a user entered number

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$