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{store +(\%x,output)}' # (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{store(\%t,ou +tput)}'
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 | |
by tye (Sage) on May 06, 2010 at 01:39 UTC |