in reply to Re^2: Spliting array into nested sequential arrays
in thread Spliting array into nested sequential arrays

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.

Replies are listed 'Best First'.
Re^4: Spliting array into nested sequential arrays
by Hameed (Acolyte) on Oct 29, 2014 at 01:34 UTC
    My assumption was that I no longer had @array1 to work with and had to create the final sorted array from @newarray.

      Never assume what is required. Trust me. Ask and ask again and preferably get the specs in writing. If the spec is ambiguous, then I assure you that you will have to either do it over or have people call your code naive at best. :)

      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
        Oh of course, I get that... :D

        This was an interview situation and it was not just an assumption but kind of a logical thought process from their question. "Now from this, do such and such..."