in reply to suffix arrays

A reference to substr's return value has the properties you want,

my $foo = "A string to work on."; my @substrs = map {\substr $foo, $_} 0 .. length($foo) - 1; my @sorted = sort { $$a cmp $$b } @substrs;
That the reference is to the original string is shown by:
$ perl -e '$foo = "A short string\n"; $bar = \substr $foo, 0, 1; $$bar + = "The"; print $foo' The short string $

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: suffix arrays
by BrowserUk (Patriarch) on Jul 18, 2003 at 21:17 UTC

    Unfortunately, as of 5.8.0, that still doesn't work properly.

    perl> print join "\n", map{ \substr 'The quick brown fox', $_ } 0 .. +19 LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4) LVALUE(0x15d7cf4)

    As you can see, each time you take an Lvalue from substr, it re-uses the same address. So you end up with and array of pointers to the last char in the string.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      Hmmm... In 5.8.1-RC2,

      perl -e'my $foo = "A string"; my @foo = map{\substr $foo, $_} 0..7; pr +int map {$$_,$/} @foo;' A string string string tring ring ing ng g $
      5.8.1 does more of what I mean!

      After Compline,
      Zaxo

        Ah! I only just got my 5.8.1 to build, so I haven't had chance to try it out there yet. Someone did mention that the p5p guys were working on a fix when this came up before, but that was way back before 5.8 was released and it was one of the first things I checked for in 5.8 but was disappointed. Once I've convinced myself that my build of 5.8.1 is reasonably safe to use, that will allow me to finish off a module that was almost ready for release 6 months ago.

        It appears the fix just made it into the wild. Thanks, its nice to know, and my apologies for casting aspersions.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller