extend list according to its elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zcabeli
    New Member
    • Jan 2008
    • 51

    extend list according to its elements

    i'm looking for a way to extend a list of 2 elements that represent boundaries, to the entire range.


    for example: input list = [1 ,5,10,15]
    output list would be = [1, 2, 3, 4 ,5 , 10 , 11 , 12 ,13 ,14 ,15]


    i can make it using the following semi-pseudo code, but perhaps there is an easier way, more elegant...

    [CODE=perl]$i = 0;
    while $i != length(list)
    for ($j = list($i); j++; j<list($i+1)) {
    push(@out_list, $j);
    };
    $i += 2;
    };[/CODE]
    Last edited by eWish; Apr 22 '08, 01:36 PM. Reason: Please use code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    Originally posted by zcabeli
    i'm looking for a way to extend a list of 2 elements that represent boundaries, to the entire range.


    for example: input list = [1 ,5,10,15]
    output list would be = [1, 2, 3, 4 ,5 , 10 , 11 , 12 ,13 ,14 ,15]


    i can make it using the following semi-pseudo code, but perhaps there is an easier way, more elegant...

    $i = 0;
    while $i != length(list)
    for ($j = list($i); j++; j<list($i+1)) {
    push(@out_list, $j);
    };
    $i += 2;
    };
    Though logic remains the same, you may reduce the number of lines of code by using this:
    Code:
    for($i=0;$i<@list;$i+=2) {
    push @out_list,($list[$i]..$list[$i+1]);
    }

    Comment

    Working...