Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: split string into hash of hashes...

by LanX (Canon)
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

UPDATE

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!

HTH

UPDATE

this might delete older sub-structures from previous paths

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

so better do

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


Comment on Re: split string into hash of hashes...
Select or Download Code
Re^2: split string into hash of hashes...
by bcarroll (Monk) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-09-22 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls