CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Finding the BEGINNING and ENDING positions of sequentially increasing sublists from a perl numeric array

 



teknokid1
New User

Nov 9, 2011, 6:12 AM

Post #1 of 3 (2647 views)
Finding the BEGINNING and ENDING positions of sequentially increasing sublists from a perl numeric array Can't Post

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
. .
. .
. .
. .


rovf
Veteran

Nov 10, 2011, 2:11 AM

Post #2 of 3 (2604 views)
Re: [teknokid1] Finding the BEGINNING and ENDING positions of sequentially increasing sublists from a perl numeric array [In reply to] Can't Post

Ist this a *Perl* question, or a general programming question?


BillKSmith
Veteran

Nov 16, 2011, 6:10 AM

Post #3 of 3 (2571 views)
Re: [teknokid1] Finding the BEGINNING and ENDING positions of sequentially increasing sublists from a perl numeric array [In reply to] Can't Post

I only know of one very special case that this solution will not handle correctly.


Code
use strict; 
use warnings;
use Readonly;
Readonly::Array my @array => (
1,2,3,4,5,6,
1,2,3,4,
1,2,1,2,3,4,5,6,7,8,9,
);
Readonly::Scalar my $COMMA => q(,);

my $beg = 0;
my $prev = 0;
my $position = -1;
foreach my $elem (@array,0) {
if ($elem < $prev) {
my $end = $position;
my $substring = join $COMMA, @array[$beg..$end];
print "($substring) BEG=$beg END=$end\n";
$beg = $position + 1;
}
$prev = $elem;
$position++;
}

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives