in reply to Lexicographic tree

I don't like your eval solution, so here are some alternatives. The first solution is yours, the second used the Data::Diver module, the third is just a plain loop.

use warnings; use strict; use Data::Dump::Streamer; my @word = map { lc } qw" the in of to that "; # solution in OP my %x; for (my @w = @word) { s/(.)/\{\1\}/g; eval("\$x${_}{EOS}++"); } Dump(\%x); # module use Data::Diver "DiveVal"; my %y; for (@word) { DiveVal(\%y, split(//, $_), "EOS")++; } Dump(\%y); # by hand my %z; for (@word) { my $t = \%z; for my $k ($_ =~ /./gs) { $t = \%{$$t{$k}} } $$t{EOS}++; } Dump(\%z); __END__

Output:

\1 better written as $1 at a.pl line 10. $HASH1 = { i => { n => { EOS => 1 } }, o => { f => { EOS => 1 } }, t => { h => { a => { t => { EOS => 1 } }, e => { EOS => 1 } }, o => { EOS => 1 } } }; $HASH1 = { i => { n => { EOS => 1 } }, o => { f => { EOS => 1 } }, t => { h => { a => { t => { EOS => 1 } }, e => { EOS => 1 } }, o => { EOS => 1 } } }; $HASH1 = { i => { n => { EOS => 1 } }, o => { f => { EOS => 1 } }, t => { h => { a => { t => { EOS => 1 } }, e => { EOS => 1 } }, o => { EOS => 1 } } };

Replies are listed 'Best First'.
Re^2: Lexicographic tree
by pierre.marchal (Novice) on May 05, 2010 at 11:09 UTC

    Anyway, thanks for your contributions. I'll make one-liners out of those :)

      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        

        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.

Re^2: Lexicographic tree
by Anonymous Monk on May 05, 2010 at 10:59 UTC

    I think you didn't understand the purpose of my post. I'm talking about a perl one-liner.
    It goes without saying that I wouldn't use such approach in a proper perl program.