Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

(jcwren) RE: varying length multi-dimensional hashes

by jcwren (Prior)
on Oct 04, 2000 at 23:16 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://35313]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-16 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found