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 | |
by tye (Sage) on May 05, 2010 at 17:57 UTC | |
by pierre.marchal (Novice) on May 05, 2010 at 22:23 UTC | |
by tye (Sage) on May 06, 2010 at 01:39 UTC | |
Re^2: Lexicographic tree
by Anonymous Monk on May 05, 2010 at 10:59 UTC |