Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

arrays and loop through array

by RedGrinGo (Novice)
on Mar 06, 2011 at 12:17 UTC ( #891672=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on arrays and loop through array
Download Code
Re: arrays and loop through array
by philipbailey (Chaplain) on Mar 06, 2011 at 12:33 UTC

    You could use eval, as follows:

    #!/usr/bin/perl use warnings; use strict; my $str = "1, 2 ,3, 4,6 -10,11 - 15,16, 17"; $str =~ s/\s+//g; $str =~ s/-/\.\./g; my @arr = eval $str; for (@arr){ my $i = $_; print "==>>> $i\n"; }

    You need to be very sure that you have cleaned up arbitrary user input before using eval in this way.

Re: arrays and loop through array
by Perlbotics (Abbot) on Mar 06, 2011 at 12:34 UTC

    Replacing

    $str =~ s/-/\.\./g;
    by
    $str =~ s/(\d+)-(\d+)/join(",",$1..$2)/ge;
    seems to do what you want... (on correct input: positive numbers, first one lesser than second).

Re: arrays and loop through array
by toolic (Chancellor) on Mar 06, 2011 at 13:28 UTC
Re: arrays and loop through array
by CountZero (Bishop) on Mar 06, 2011 at 16:44 UTC
    Don't re-invent the wheel, use Parse::Range:
    use Modern::Perl; use Parse::Range qw(parse_range); my $str = "1, 2 ,3, 4,6 -10,11 - 15,16, 17"; my @arr = parse_range($str); say "@arr";
    Output: 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: arrays and loop through array
by johngg (Abbot) on Mar 06, 2011 at 17:45 UTC

    Please note that the replacement part of a s/this/that/ is not a regular expression so the dots do not need to be escaped.

    knoppix@Microknoppix:/media/sda1/perl/Monks$ perl -E ' > $str = q{6 - 10, 14 - 17, 19}; > $str =~ s{-}{..}g; > say $str;' 6 .. 10, 14 .. 17, 19 knoppix@Microknoppix:/media/sda1/perl/Monks$

    I hope this is helpful.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://891672]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-09-18 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls