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 } } };

In reply to Re: Lexicographic tree by ambrus
in thread Lexicographic tree by pierre.marchal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.