baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

well I'm trying to write a program that needs to sort the suffixes of an array. Let A be an array of characters:

@A = (A,C,D,D,C,D,A,G,F);
now to sort the suffixes I would naively brake and array into 9 suffix arrays and the sort them out and get something like this:
 
ACDD...
AGF
CDA...
CDD...
DAG...
....


now, how do i do this without chopping thees into sub-arrays, and just do it via references like in c, where you do this through pointers (references).

thnx

baxy

UPDATE:

well thnx ppl for the quick help but there is still a problem that i was not aware of ..... so in the following code i get a strange solution:

#!/usr/bin/perl use strict; my @array = qw(a c a a a c a t a t z); my @sorted = sort { $array[$a] cmp $array[$b] } (0 .. $#array); print join('',@array[$sorted[$_] .. $#array]) . "\n" for 0..10; #prints acaaacatatz aaacatatz aacatatz acatatz atatz atz caaacatatz catatz tatz tz z
isn't "acaaacatatz" lexicographically lower then "aacatatz" ???????(SOLVED : see the code below) Oh , I just saw your other comments , so what i'm playing arround is Kasai's algorithm so the whole code is : ---

UPDATE2 : all corrected and below is the working code if anyone needs is, it is a bit naive but illustrates the point and it works Code was moved to : Kasai's algorithm so if someone is looking for it, well here it is , in Perl !!!!

Replies are listed 'Best First'.
Re: How to sort the referencese
by johngg (Canon) on Aug 10, 2011 at 10:00 UTC

    Your question is a little unclear but my guess from your partial output is that you want something like this.

    use strict; use warnings; use 5.010; my @arr = qw{ A C D D C D A G F }; my $str = join q{}, @arr; say for sort map { substr $str, $_ } 0 .. length( $str ) - 1;

    The output.

    ACDDCDAGF AGF CDAGF CDDCDAGF DAGF DCDAGF DDCDAGF F GF

    I hope I have guessed correctly and that this is helpful.

    Update: A version that works on array subscripts rather than offsets into a joined string, using the revised data in the OP.

    use strict; use warnings; use 5.010; my @arr = qw{ a c a a a c a t a t z }; my @sortedIndices = map { $_->[ 0 ] } sort { $a->[ 1 ] cmp $b->[ 1 ] } map { [ $_, join q{}, @arr[ $_ .. $#arr ] ] } 0 .. $#arr; say join q{}, @arr[ $sortedIndices[ $_ ] .. $#arr ] for 0 .. $#sortedIndices;

    Outputs

    aaacatatz aacatatz acaaacatatz acatatz atatz atz caaacatatz catatz tatz tz z

    Cheers,

    JohnGG

Re: How to sort the referencese
by jethro (Monsignor) on Aug 10, 2011 at 09:14 UTC

    You can't do the same thing as in C. But you can create a second array with the sorted indices:

    my @A = qw(A C D D C D A G F); my @sorted = sort { $A[$a] cmp $A[$b] } (0 .. $#A); print @sorted,"\n"; print @A[$sorted[1] .. $#A],"\n"; print @A[$sorted[4] .. $#A],"\n"; # prints 061423587 AGF DDCDAGF
Re: How to sort the referencese
by Utilitarian (Vicar) on Aug 10, 2011 at 09:07 UTC
    Hi baxy, I think you're looking for array slices

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."