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

hi guys, what is the most efficient way to code in perl having this input value of : 7-12 ...then, if will have an output of: 7 8 9 10 11 12 can this be done without a loop ? thanks for you help in advance. - gerry

Replies are listed 'Best First'.
Re: numeric manipulation
by Eliya (Vicar) on Mar 11, 2011 at 18:43 UTC
    my $input = "7-12"; my ($start, $end) = split /-/, $input; my @output = $start..$end; print "@output\n"; # 7 8 9 10 11 12
Re: numeric manipulation
by BrowserUk (Patriarch) on Mar 11, 2011 at 18:43 UTC

    This is one of several ways to do this, but if it is homework, you'd better hit PerlDoc and make sure you can explain how your chosen solution works:

    $input = '7-12'; $input =~ /(\d+)-(\d+)/ and print $1 .. $2 or die 'Bad input';; 7 8 9 10 11 12

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: numeric manipulation
by Fletch (Bishop) on Mar 11, 2011 at 18:53 UTC

    See also Set::IntSpan

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      ahahahhahahahahahah
Re: numeric manipulation
by CountZero (Bishop) on Mar 11, 2011 at 18:56 UTC
    use Modern::Perl; use Parse::Range; my @range = parse_range('7-12'); say "@range";

    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: numeric manipulation
by FunkyMonk (Bishop) on Mar 11, 2011 at 19:02 UTC
    The previous replies were all too pretty...

    perl -wE '$_ = "7-12"; say join " ", [/(\d+)-/]->[0]..[/-(\d+)/]->[0]'

    :-)

    Update: slightly less ugly?

    perl -wE '$_ = "7-12"; say join " ", (/(\d+)-/)[0]..(/-(\d+)/)[0]'
Re: numeric manipulation
by JavaFan (Canon) on Mar 11, 2011 at 19:35 UTC
    can this be done without a loop
    No. There will always be a loop, even if it's not immediately obvious.