in reply to Re: Is it possible to make reference to substrings and sort them?
in thread Is it possible to make reference to substrings and sort them?

If you can spare the memory to double your text, you can create references to all your rotations. Below I use the Schwartzian transform to create the references once, then sort them.

my $text2 = "$text$text"; my $bwt4 = join('', map{substr($text, ($_ - 1), 1)} map{ $_->[0] } sort{ ${$a->[1]} cmp ${$b->[1]} } map{ [$_,\substr($text2,$_,$ntext)] } 0..$ntext-1 ); print "$text: $bwt4\n";
  • Comment on Re^2: Is it possible to make reference to substrings and sort them?
  • Download Code

Replies are listed 'Best First'.
Re^3: Is it possible to make reference to substrings and sort them?
by Anonymous Monk on Mar 22, 2015 at 19:17 UTC

    Good call on doubling the string. Though the Schwartzian seems unnecessary here:

    my $text2 = "$text$text"; my $tlen = length($text); my $bwt7 = join('', map{ substr($$_, -1) } sort{ $$a cmp $$b } map{ \substr($text2, $_, $tlen) } 0..$tlen-1 ); print "$text: $bwt7\n";

    Intermediate array of references consumes more memory than an array of offsets would, I think, but is faster to sort. Something to test and benchmark...