in reply to Spliting array into nested sequential arrays

Well, just looking at it I think your first given output is wrong. Shouldn't it be this:

Output: [[1,2,3,4,5,6,7,8],[3,5,6],[3,8],[1,7,8]]

It appears that the goal is to start a new array each time the next value is lower than the previous one.

That said, it would help if you would give the desired goal as described to you since I am only guessing at it for now.

You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Replies are listed 'Best First'.
Re^2: Spliting array into nested sequential arrays
by Hameed (Acolyte) on Oct 29, 2014 at 01:15 UTC
    That is correct. Sorry there was a typo in the square bracket entity. Fixed it now.

      Without knowing the exact question posed, you did not need to push stuff onto @array3. All you had to do was this for that: my @array3 = sort(@array1);

      As for generating the sub-arrays, give me a few minutes and I'll have something different than using indexes.

      Update: working code.

      #!/usr/bin/perl use warnings; use strict; use Data::Dumper; $Data::Dumper::Indent = 1; my @array1 = (1,2,3,4,5,6,7,8,3,5,6,3,8,1,7,8); my @array3 = sort(@array1); my @subarrays = ( [] ); my $last = 0; for ( @array1 ) { push(@subarrays, []) if $_ < $last; push(@{$subarrays[-1]},$_); $last = $_; } print Dumper(\@array1); print Dumper(\@subarrays); print Dumper(\@array3); exit; __END__
      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
        My assumption was that I no longer had @array1 to work with and had to create the final sorted array from @newarray.