Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: split string into hash of hashes...

by LanX (Chancellor)
on Mar 03, 2013 at 02:05 UTC ( #1021480=note: print w/replies, xml ) Need Help??

in reply to split string into hash of hashes...

first of all, this is not possible

$hash{'HKEY_LOCAL_MACHINE'}{'SOFTWARE'}{'Vendor'}{'Product'}{'CurrentV +ersion'}{' +Tokens'}{'Encotone'}{'SerialNumberUserAttribute'}=12345; $hash{'HKEY_LOCAL_MACHINE'}{'SOFTWARE'}{'Vendor'}{'Product'}{'CurrentV +ersion'}{' +Tokens'}{'Encotone'}{'SerialNumberUserAttribute'}{'LanMan'}{'Value'}= +"";

a hash-entry can't be 12345 and a hash-ref to {'LanMan'}... at the same time.

consider ... {value}=12345

> Anybody have any ideas?

first step should be to build the mainpath as string with splits and joins.

then eval this string to autovivify this hash, and assign a ref to a $subhash to it.

This $subhash -ref can be populated now with all entries which come.

Cheers Rolf


if you don't like evals build the HoH-path in a loop (line 163)

DB<160> $line='HKEY_LOCAL_MACHINE\SOFTWARE\Vendor\Product\CurrentVer +sion\Tokens\Encotone\SerialNumberUserAttribute=12345' DB<161> ($path,$value)= split /=/, $line DB<162> $subhash=$hash={} DB<163> $subhash = $subhash->{$_} = {} for split /\\/,$path DB<165> $subhash->{value}=$value DB<166> $subhash->{LanMan}{value}="" DB<167> $subhash->{LanMan}{type}="REG_SZ" #... and so on for all sub-entries ... DB<168> $hash => { HKEY_LOCAL_MACHINE => { SOFTWARE => { Vendor => { Product => { CurrentVersion => { Tokens => { Encotone => { "SerialNumberUserAttribute +" => { LanMan => { type => "REG_SZ", value => "" }, value => 12345 }, }, }, }, }, }, }, }, }

now repeat these steps for all paths!



this might delete older sub-structures from previous paths

DB<163> $subhash = $subhash->{$_} = {} for split /\\/,$path

so better do

$subhash = $subhash->{$_} //= {} for split /\\/,$path

Replies are listed 'Best First'.
Re^2: split string into hash of hashes...
by bcarroll (Pilgrim) on Mar 03, 2013 at 04:23 UTC
    Thanks for the reponses, I realize now that my manual example is flawed...
      > I realize now that my manual example is flawed...

      Line 165 in my code solved this. You need a dedicated key in a subhash for "direct" values and your fine.

      It's disputable if "value" is a good choice for a key-name, but that's up to you.

      Cheers Rolf

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2016-10-26 17:18 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (344 votes). Check out past polls.