Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

(jcwren) RE: varying length multi-dimensional hashes

by jcwren (Prior)
on Oct 04, 2000 at 23:16 UTC ( #35313=note: print w/ replies, xml ) Need Help??


in reply to varying length multi-dimensional hashes

I'll leave the reading of the hash as an exercise to the user, but here's a fun way to populate the hash:

Update: Apparently, here's a really slow, dangerous, stupid, fraught with peril, performancing dehancing way to do it. Oh well, some code just sucks, don't it?

#!/usr/local/bin/perl -w use strict; use Data::Dumper; { my %hash = (); while (<DATA>) { my $s; $s .= "{'$_'}" foreach (/(...)/g); eval ('$hash ' . $s . '++'); die $@ if $@; } print Dumper ([\%hash]); } __DATA__ aaabbbcccddd aab aacbbb aaabbacca aaabbaccb ababbbddd

$VAR1 = [ { 'aab' => 1, 'aba' => { 'bbb' => { 'ddd' => 1 } }, 'aac' => { 'bbb' => 1 }, 'aaa' => { 'bba' => { 'cca' => 1, 'ccb' => 1 }, 'bbb' => { 'ccc' => { 'ddd' => 1 } } } } ];
--Chris

e-mail jcwren


Comment on (jcwren) RE: varying length multi-dimensional hashes
Select or Download Code
RE: (jcwren) RE: varying length multi-dimensional hashes
by merlyn (Sage) on Oct 04, 2000 at 23:25 UTC
    I voted this one down because
    • it's an unnecessary use of runtime-string-compilation eval
    • it breaks on general data, so it's not a good general pattern, and you didn't add that disclaimer to your text. Hint: what if your data contained single quotes?
    Others have posted faster and more general solutions, so I won't include one of mine here. But avoid this eval solution, please.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://35313]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-12-18 00:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls