in reply to Grabbing slices
Well, if there is a perl-ish way to do it, I just don't know how to do it. Here is the simple loop over array while storing the start and end of each char way:
@array=qw(x x y z z z); $c=$array[0]; $start=0; $end=0; for $index (1..$#array){ if (!($c eq $array[$index])){ push(@output, $start, $end); $c=$array[$index]; $start=$index; $end=$index; }else{ $end=$index; } } push(@output, $start, $end); print join(" ", @output), "\n"; Outputs: 0 1 2 2 3 5
I'm sure there someone else can probably do better.
Cheers
|
|---|