in reply to Re^2: Lexicographic tree
in thread Lexicographic tree

I'd make two minor adjustments to your one-liner plus one major one and fix one bug:

perl -MStorable -ne 's/(.)/\{$1\}/g;eval­("\$x$_\{­EOS}=0");END{sto­re +(\%x,out­put)}' # (Perl not sed)^ prettier^^^ bug^^ ^^^^was quit +e wasteful

Regarding

I'll make one-liners out of those :)

Just because it is a one-liner doesn't mean it has to suck as code (using string-eval like that). ;) The approaches I've used (for example in Re: find all paths of length n in a graph (Boggle solver)) fit easily as one-liners. I've replaced your 'EOS' with a more universal "end of" string, "\n".

perl -MStorable -ne '$p=\%t;$p=$p->{$_}||={}for/./gs;END{sto­re(\%t,ou +t­put)}'

That doesn't even autovivify.

Here's another version for those who don't care about the 'store to file' step (with extra verbosity to overcome Data::Dumper's extremely ugly defaults and to provide sample input). Note that here I use an earlier approach of mine so I can have the end-of-word entry point to a false value instead of to an empty hash.

grep '^s\?[hk]\?[ei][ln][tlk]y\?$' /usr/share/dict/words | perl -MData +::Dumper -ne '$p=\\%t;$p=\$$p->{$_}for/./gs;$$p=0;END{print Data::Dum +per->new([\%t])->Terse(1)->Indent(1)->Sortkeys(1)->Useqq(1)->Dump()}' + | less

- tye        

Replies are listed 'Best First'.
Re^4: Lexicographic tree (betters)
by pierre.marchal (Novice) on May 05, 2010 at 22:23 UTC

    Hi tye

    s/(.)/\{$1\}/g
    Well. I don't remember why I used \1 instead of $1 but you're true : this is Perl code.

    eval­("\$x$_\{­EOS}=0");
    With $_ I get this warning:

    Use of uninitialized value $_{"EOS"} in concatenation (.) or string at -e line 1, <> line X.
    + the code produces no data
    With ${_} everything works just fine...

    Considering the affectation {­EOS}=0, I wouldn't call it a bug as I didn't mean to get the string "EOS" in my tree.

    END{sto­re(\%x,out­put)}
    +1! You're right. It was quite wasteful :|

    Thanks for your help.

      eval­("\$x­$_\{­EOS}=­0");

      With $_ I get this warning:

      Use of uninitialized value $_{"EOS"} in concatenation (.) or strin +g at -e line 1, <> line X.

      Works for me. Based on the error, it looks like you failed to include the \ between $_ and {EOS}.

      Considering the affectation {­EOS}=0, I wouldn't call it a bug as I didn't mean to get the string "EOS" in my tree.

      If you didn't want that, then you should replace your {EOS} with ={} so the intent is clear instead of looking very much like a bug.

      Even if it isn't a bug compared to your intention, it is still a bug if you are trying to make a trie. With your implementation you can't tell that both "hill" and "hilly" are stored in one structure.

      - tye