RedGrinGo has asked for the wisdom of the Perl Monks concerning the following question:
Hi All
I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-".
Example: 1,2,3,4-9,12,15,34-36,70
Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above the loop use the array element as string.
my $str = "1, 2 ,3, 4,6 -10,11 - 15,16, 17"; $str =~ s/\s+//g; $str =~ s/-/\.\./g; my @arr = split /,/, $str; for (@arr){ my $i = $_; print "==>>> $i\n"; }
The result of this is: ==>>> 1 ==>>> 2 ==>>> 3 ==>>> 4 ==>>> 6..10 ==>>> 11..15 ==>>> 16 ==>>> 17 and not: ==>>> 1 ==>>> 2 ==>>> 3 ==>>> 4 ==>>> 6 ==>>> 7 ==>>> 8 ==>>> 9 ==>>> 10 ==>>> 11 ==>>> 12 ==>>> 13 ==>>> 14 ==>>> 15 ==>>> 16 ==>>> 17 as wanted...
Any suggestions? Thank you all.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: arrays and loop through array
by Perlbotics (Archbishop) on Mar 06, 2011 at 12:34 UTC | |
|
Re: arrays and loop through array
by philipbailey (Curate) on Mar 06, 2011 at 12:33 UTC | |
|
Re: arrays and loop through array
by CountZero (Bishop) on Mar 06, 2011 at 16:44 UTC | |
|
Re: arrays and loop through array
by toolic (Bishop) on Mar 06, 2011 at 13:28 UTC | |
|
Re: arrays and loop through array
by johngg (Canon) on Mar 06, 2011 at 17:45 UTC |