Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: split string into hash of hashes...

by punch_card_don (Curate)
on Mar 03, 2013 at 16:52 UTC ( #1021520=note: print w/replies, xml ) Need Help??

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

Update: Working this out for myself demonstrated to me that this is just a clunky version of Rolf's more elegant code, above. But it makes each operation more clear to my less expert eyes.

This is a case where autovivification and hashrefs are your friends. With a helping hand from some creative data structure manipulation.

You have two issues:

  1. how to create a hash of hashes without knowing the depth of the structure in advance.
  2. How to include a value for the entire dimension three levels from the bottom, knowing, as others have pointed out, that an element cannot contain at once a value and a reference to the lower levels of the structure.
The solution to (1) is autovivifciation and hash refs. The solution to (2) is to pad your hash with dummy elements down to the lowest dimension.
#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; print "Content-type:text/html\n\n"; my $line1 = 'HKEY_LOCAL_MACHINE\SOFTWARE\Vendor\Product\CurrentVersion +\Tokens\Encotone\SerialNumberUserAttribute=12345'; my $line2 = 'LanMan:= ; REG_SZ'; my @line1_parts = split(/[=\\]/, $line1); my @line2_parts = split(/:= ;/, $line2); my %hash; my $href = {}; my $i; for $i (0 .. $#line1_parts) { if ($i == 0) { # for the first time around, create the first level of the str +ucture, itself as a hash. Autovivification means that simply referri +ng to it creates it. No need to put anything in it yet. $hash{$line1_parts[$i]} = {}; #create a hash reference to that element (which is itself a ha +sh); $href = \%{ $hash{$line1_parts[$i]} }; } elsif ($i == $#line1_parts) { # last time around, create the acutal hash elements you want, +filling in down to the bottom level with dummy elements for the Seria +lNumberUserAttribute. $href->{'dummy'} = $line1_parts[$i]; $href->{$line2_parts[0]}{'Value'}=""; $href->{$line2_parts[0]}{'Type'}=$line2_parts[1]; } else { # each next time around, create the next level of the structur +e, itself as a hash, tacked onto the hash that the hash ref points to $href->{ $line1_parts[$i] } = {}; # move the hash ref to this new hash $href = \%{ $href->{$line1_parts[$i]} }; } } print "<pre>\n"; print Dumper(\%hash); print "</pre>\n";
$VAR1 = { 'HKEY_LOCAL_MACHINE' => { 'SOFTWARE' => { 'Vendor' => { 'Product' => { 'CurrentVersion' => { 'Tokens' => { 'Encotone' => { 'SerialNumberUserAttribute' => { 'LanMan' => { 'Type' => ' REG_SZ', 'Value' => '' }, 'dummy' => '12345' } } } } } } } }

Time flies like an arrow. Fruit flies like a banana.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2021-01-23 05:22 GMT
Find Nodes?
    Voting Booth?