Finding BEGINNING & ENDING positions of sequentially increasing sublists from a perl numeric array

I have got an Perl array like:

@array = (1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,5,6,7,8,9...............)

This numeric sequence will be always sequentially increasing, unless it encounters, The beginning of the new sequentially increasing numeric sequence.
SO in this array we get sequentially increasing many subsists.

There can be n numbers in this array and so can many sequentially increasing subsists.These sublists will always be increasingly sorted.

I want to extract the BEGINING and the ENDING positions of these subsists in the array with respect to array beginning position 0.

(1,2,3,4,5,6)              BEG=0  END=5
(1,2,3,4)                  BEG=6  END=9
(1,2)                      BEG=10 END=11
(1,2,3,4,5,6,7,8,9)        BEG=12 END=20
.                          .
.                          .
.                          .
.                          .
$
$
$ perl -e '@x = (1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,5,6,7,8,9);
           @y = grep {$x[$_] > $x[$_+1]} (0..$#x);
           foreach $i (0..$#y) {
             $begin = $i == 0 ? 0 : $prev+1;
             $end = $y[$i];
             printf ("%-30s    BEG=%-2s   END=%s\n", "(".join(",", @x[$begin..$end]).")", $begin, $end);
             $prev = $end;
           }
          '
(1,2,3,4,5,6)                     BEG=0    END=5
(1,2,3,4)                         BEG=6    END=9
(1,2)                             BEG=10   END=11
(1,2,3,4,5,6,7,8,9)               BEG=12   END=20
$
$
$

tyler_durden

1 Like

A solution using shell script:

#!/usr/bin/ksh
fPrint() {
if [[ ${mCnt} = 0 ]]; then
  echo '('${mList[$mI]}')' BEG=${mI} END=${mI}
else
  echo ${mOut}','${mList[$mI]}')' BEG=${mBEG} END=${mI}
fi
}
typeset -i mI=0
typeset -i mJ
typeset -i mCnt=0
typeset -i mTotal
set -A mList 1 2 3 4 5 6 6 2 3 4 1 2 1 2 3 4 5 6 7 8 9 1 1 2
mTotal=${#mList[*]}-1
while [[ ${mI} -lt ${mTotal} ]]; do
  mJ=${mI}+1
  if [[ "${mList[$mJ]}" > "${mList[$mI]}" ]]; then
    if [[ ${mCnt} = 0 ]]; then
      mOut='('${mList[$mI]}
      mBEG=${mI}
    else
      mOut=${mOut}','${mList[$mI]}
    fi
    mCnt=${mCnt}+1
  else
    fPrint
    mCnt=0
  fi
  mI=${mI}+1
done
fPrint

Another one:

$
$
$ perl -e '@x = (1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,5,6,7,8,9);
           map {
             if ($_ == 0) {
               $str = "($x[$_]";
               $bgn = 0;
             } elsif ($x[$_] > $x[$_+1]) {
               $str .= ",$x[$_])";
               printf ("%-30s    BEG=%-2s   END=%s\n", $str, $bgn, $_);
               $str = "(";
               $bgn = $_ + 1;
             } else {
               $str .= ($x[$_] < $x[$_-1] ? "" : ",")."$x[$_]";
             }
           } (0..$#x)'
(1,2,3,4,5,6)                     BEG=0    END=5
(1,2,3,4)                         BEG=6    END=9
(1,2)                             BEG=10   END=11
(1,2,3,4,5,6,7,8,9)               BEG=12   END=20
$
$
$

tyler_durden

my @array = (1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,5,6,7,8,9);
my @tmp;
for(my $i=0;$i<=$#array;$i++){
	$pre=$array[$i] unless defined $pre;
	if($array[$i]<$pre){
		print "@tmp", " START: ", $start||0, " END: $end\n";
		$pre=$array[$i];
		$start=$end=$i;
		$#tmp=-1;
		push @tmp,$array[$i];
	}
	else{
		$end=$i;
		$pre=$array[$i];
		push @tmp,$array[$i];
	}
}
if($#tmp){
	print "@tmp", " START: ", $start||0, "END: $end\n";
}

Respected durden_tyler, Shell_Life, summer_cherry,

I am a beginner and that was my first professional assignment.
I am greatly thankful for you respective replies.
Because of your valuable suggestions, I was able to finish my Task.
I always look for your guidance & Support.

Warm Regards,
Teknokid1