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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding BEGINNING & ENDING positions of sequentially increasing sublists from a perl numeric array
# 1  
Old 11-09-2011
Finding BEGINNING & ENDING positions of sequentially increasing sublists from a perl numeric array

I have got an Perl array like:
Code:
@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.
Code:
(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
.                          .
.                          .
.                          .
.                          .

Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-09-2011 at 10:49 AM.. Reason: code tags, see PM
# 2  
Old 11-09-2011
Quote:
Originally Posted by teknokid1
I have got an Perl array like:
Code:
@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.
Code:
(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
.                          .
.                          .
.                          .
.                          .

Code:
$
$
$ 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
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 11-09-2011
A solution using shell script:
Code:
#!/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

# 4  
Old 11-09-2011
Another one:

Code:
$
$
$ 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
# 5  
Old 11-10-2011
Code:
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";
}


Last edited by Franklin52; 11-10-2011 at 04:37 AM.. Reason: Code tags
# 6  
Old 11-14-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add specific bases at the beginning and ending of all the fasta sequences?

Hi, I have to add 7 bases of specific nucleotide at the beginning and ending of all the fasta sequences of a file. For example, I have a multi fasta file namely test.fasta as given below test.fasta >TalAA18_Xoo_CIAT_NZ_CP033194.1:_2936369-2939570:+1... (1 Reply)
Discussion started by: dineshkumarsrk
1 Replies

2. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

3. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

4. Shell Programming and Scripting

Finding File Names Ending In 3 Random Numerical Characters

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190 each of this files is in a directory named for the file but excluding the extension. Now the last three numeric characters, in this case 999, can be anything from 001 to 999, I need to... (3 Replies)
Discussion started by: roche.j.mike
3 Replies

5. UNIX for Dummies Questions & Answers

using sed to write horizontally & sequentially in a file

Hey there I have two commands to get exactly the data i want but.. i want to write them into a file side by side and in the same order so that they always match. So what i'm hoping to learn from this thread is some of the different ways one could take the output of grepped data and write them in... (7 Replies)
Discussion started by: phpfreak
7 Replies

6. Shell Programming and Scripting

Finding missing files that are named sequentially with Perl?

Hello I am new to Perl, in fact I am on chapter one of the book. :) However I am in need of a Perl Script faster than I can finish the book. Perhaps someone can help me with my immediate need while I read my book. I have a directory with hundreds of files that are all named like... (4 Replies)
Discussion started by: newftronics
4 Replies

7. Programming

numeric values ending in 'U'

I am getting back on the C++ programming after many years away. I recently received an SDK that has code like this where numeric values end in 'U'. What does this mean? if ((ptr % 16U) == 0U) return buffer; (3 Replies)
Discussion started by: sneakyimp
3 Replies

8. UNIX for Dummies Questions & Answers

Copying files multiple times increasing sequentially

I am looking for the easiest way to copy a set of files 1000 times and increment sequentially. I want to copy these 2 files: Scenario.1.1.ud Scenario.1.2.ud So that it creates the following: Scenario.2.1.ud Scenario.2.2.ud Scenario.3.1.ud Scenario.3.2.ud .. .. Scenario.1000.1.ud... (2 Replies)
Discussion started by: JPOrlando
2 Replies

9. Shell Programming and Scripting

hw to insert array values sequentially in a file

Hi All :), I am very new to unix. I am requiring ur help in developing shell script for below problem. I have to replace the second field of file with values of array sequentially where first field is ValidateKeepVar <File> UT-ExtractField 1 | &LogEntry &Keep(DatatoValidate)... (3 Replies)
Discussion started by: rohiiit.sharma
3 Replies

10. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies
Login or Register to Ask a Question