Heres my go. Im not sure i want to think about golfing while it needs to handle error cases. If we can assume that all inputs are valid (and a better input format) then golfing it might be interesting.

Update: I didnt test my original solution properly. Its actually broken. Ive stuck it in a comment if you want to see my foolishness and i've posted a new version.

The updated code. Ive posted it three times, pre-golf, golf with error handling, and golf without error handling. golf with error handling (including error message) is 180 chars, golf without error handling is 126. Ive not counted the sub declaration itself. (ie the sub NAME { } is excluded for the count)

use strict; use warnings; use Data::Dumper; use Carp; sub subrange_pre_golf { my ($r1,$r2,$ary,$s,$f)=@_; ($s,$f)=(0,$#$ary) unless $s && $f; ($_<$s && $_<$f or $_>$s && $_>$f) and die "Bad range!" for $r1,$r2; my ($dx,$idx,@ret)=($r2 <=> $r1); $idx=($s < $f) ? -$s+$r1 : ($dx=-$dx,$s-$r1); while ( @ret <= abs($r2-$r1) ) { push @ret,$ary->[$idx]; $idx+=$dx; } return @ret; } # Excluding sub {} wrapper its 126 bytes. # 1 2 3 4 5 6 +7 #123456789012345678901234567890123456789012345678901234567890123456789 +012345678 sub subrange_no_error { my($p,$q,$Y,$s,$f)=@_;my($d,$i,@r)=($q<=>$p);$i=($s<$f)?-$s+$p:($d=-$ +d,$s-$p); push(@r,$Y->[$i]),$i+=$d while@r<=abs($q-$p);@r; } #180 bytes (counting newline used as mandatory whitespace as one char) sub subrange_golf_error { my($p,$q,$Y,$s,$f)=@_;my($d,$i,@r)=($q<=>$p);$i=($s<$f)?-$s+$p:($d=-$ +d,$s-$p); ($_<$s&&$_<$f||$_>$s&&$_>$f)&&die"Bad range!"for$p,$q;push(@r,$Y->[$i +]),$i+=$d while@r<=abs($q-$p);@r; } # this is now just a wrapper to subrange. Its just for testing and int +erface # compatibility sub arrange { my $href=shift; my @ret= subrange_golf_error(@{$href->{subrange}},$href->{arr},@{$hr +ef->{range}}); $href->{ret}=\@ret; unless (defined wantarray) { my $idx=$href->{range}[0]; my $dx=$href->{range}[1] <=> $href->{range}[0]; print "Subrange @{$href->{subrange}} of Array [". join(", ", map{ my $r="$idx:$_"; $idx+=$dx; $r } @{$href->{a +rr}}) ."] is <@ret>\n"; } else{ return wantarray ? @ret : \@ret; } } foreach my $t ([5,8],[8,5],[6,5],[6,7],[5,6],[7,6],[1,2],[8,9]) { foreach my $r ([8,5],[5,8]) { eval { arrange({ arr => [qw(cero uno dos tres)], range => $r, subrange =>$t }); 1 } or warn "Test @$t failed on range @$r:\n$@"; } }

Cheers

---
demerphq


In reply to Re: Code challenge: array subrange (updated with golf and corrections) by demerphq
in thread Code challenge: array subrange by spurperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.