spooknick has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I am trying to implement Burrows-Wheeler transform (BWT) and this is the code I have:
my $bwt = join('', map{substr($text, ($_ - 1), 1)} sort{ (substr($text,$a).substr($text, 0, $a)) cmp (substr($text, $b).substr($text,0, $b)) } 0..length($text)-1 );
It is totally functional, but it gets problematic when the text is large.
I suspect, that the sort function here above is making all the cyclic rotations first and then sort them. If that's the case,
is it possible to make references to the cyclic rotations without actually making them, and use those in the sort function?
Any remarks, suggestions are welcomed.
For those that are not familiar with, the BWT is done by making all cyclic rotation of text, sorting them, and keeping the last char of each rotation. BWT of text "BANA$" is done as following:
Cyclic rotations:
BANA$
ANA$B
NA$BA
A$BAN
$BANA
Which are sorted as:
$BANA
A$BAN
ANA$B
BANA$
NA$BA
Keeping the last chars gives BWT = ANB$A
|
|---|