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

Hi,

How do I make an array from for instance 45-48?

Input 45-48, 88-99, 102-124, ...

needed output: 45,46,47,48 and so on ...

/Hossein

Replies are listed 'Best First'.
Re: do 45-47 to array 45,46,47
by rnewsham (Curate) on Jul 01, 2013 at 12:35 UTC

    If you want to change 45-48 into 45,46,47,48 you can do this.

    use strict; use warnings; while ( <DATA> ) { if ( m/^(\d+)-(\d+)$/ ) { for my $value ( $1 .. $2 ) { print "$value,"; } } } __DATA__ 45-48 88-99 102-124
    Output 45,46,47,48,88,89,90,91,92,93,94,95,96,97,98,99,102,103,104,105,106,10 +7,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124 +,

      Thank you :)

      /Hossein
Re: do 45-47 to array 45,46,47
by rjt (Curate) on Jul 01, 2013 at 12:27 UTC
Re: do 45-47 to array 45,46,47
by Lawliet (Curate) on Jul 01, 2013 at 12:39 UTC

    Problem: You are given the input string "88-99", and you need to output each number from 88 to 99, inclusive.

    $ perl -E'my ($lower, $upper) = split(/-/, pop); say for $lower .. $up +per' 88-99 88 89 90 91 92 93 94 95 96 97 98 99

    The key here is the range (..) operator. From perldoc, the range operator returns a list of values counting (up by ones) from the left value to the right value.

      Thank you :) /Hossein
Re: do 45-47 to array 45,46,47
by daxim (Curate) on Jul 01, 2013 at 12:54 UTC
    Shortest code is best code! Use Set::IntSpan to handle your newsrc intspans.
    Set::IntSpan->new('45-48, 88-99, 102-124')->elements
    Return value is the list:

      "Shortest code is best code!"

      I guess that depends on what you consider the totlal count of lines to be. The module you use is 2737 lines long ;)

Re: do 45-47 to array 45,46,47
by hdb (Monsignor) on Jul 01, 2013 at 12:57 UTC

    Or with eval:

    use strict; use warnings; my $_ = "45-48, 88-99, 102-124"; my @result = map { s/-/../; eval $_ } /(\d+-\d+)/g; print "@result\n";
Re: do 45-47 to array 45,46,47
by Hossein (Acolyte) on Jul 02, 2013 at 06:51 UTC
    Thank you all for you help :)