Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: deeply nested hashes of arrays of hashes of...

by hdb (Parson)
on Mar 26, 2014 at 19:23 UTC ( #1079856=note: print w/ replies, xml ) Need Help??


in reply to deeply nested hashes of arrays of hashes of...

Here is a little script that tells you for each element what it is and how to address it:

use strict; use warnings; sub refClean { my @s = split /\n/, shift; my $m = 0; for my $line (@s) { next unless $line =~ /(.+)#__/; my $len = length($1); $m=$len>$m?$len:$m; } for my $line (@s) { next unless $line =~ /(.+)#__/; my $len = length($1); $line =~ s/#__/" " x ($m-$len)."#"/e; } return join "\n", @s; } sub refStructure { my $element = shift; my $level = shift; my $tab = shift; my $name = shift; my $s = $tab x $level; my $ctr = 0; my $r = ref($element); if( !$r ) { $s .= "'$element', #__ scalar: ".$name."\n"; } elsif( $r eq 'HASH' ) { $s .= "{ #__ ref to hash: ".$name."\n"; for my $key (keys %$element) { $s .= $tab x ($level+1)."'$key' => "; $s .= " #__ key of hash, string: +(keys %{".$n +ame."})[".$ctr++."]\n"; $s .= refStructure( $element->{$key}, $level+2 +, $tab, $name."->{'".$key."'}" ); } $s .= $tab x $level."}\n"; } elsif( $r eq 'ARRAY' ) { $s .= "[ #__ ref to array: ".$name."\n"; map { $s .= refStructure( $_, $level+1, $tab, $name."- +>[".$ctr++."]" ) } @$element; $s .= $tab x $level."]\n"; + } else { $s .= "$r #__ unknown type of reference\n"; } $s = refClean( $s )."\n" unless $level; return $s; } my $VAR1 = { 'IsLocked' => {}, 'ID' => '18827', 'TransactionID' => 'eb8xxx8-xxxb-4xx7-baed-xxx66c790c3', 'Name' => 'Production - xxxx', 'FarmRoleSet' => { 'Item' => [ { 'IsScalingEnabled' => '1', 'ServerSet' => { 'Item' => { 'Uptim +e' => '1452.4', 'Serve +rID' => 'xxxxc-487f-9281-0ebf39d2d4ea', 'Statu +s' => 'Running', 'Exter +nalIP' => '54.193.71.108', 'Index +' => '1', 'Inter +nalIP' => '1.1.11.106', 'Scala +rizrVersion' => '2.5.2', 'Platf +ormProperties' => { + 'InstanceType' => 'm3.medium', + 'InstanceID' => 'i-xxxx43', + 'AMIID' => 'ami-xxxxxe97', + 'AvailabilityZone' => 'us-west-1b' + }, 'IsDbM +aster' => '0' } }, 'ID' => '68608', 'RoleID' => '62343', 'Platform' => 'ec2', 'CloudLocation' => 'us-west-1', 'Name' => 'prod-app-xxx-v2', 'Category' => 'Application Serv +ers', 'PlatformProperties' => { 'Instan +ceType' => 'm3.medium', 'Availa +bilityZone' => 'x-scalr-diff' }, 'ScalingAlgorithmSet' => {}, 'ScalingProperties' => { 'MaxInst +ances' => '3', 'MinInst +ances' => '1' } }, { 'IsScalingEnabled' => '1', 'ServerSet' => { 'Item' => [ { 'Upt +ime' => '4438.55', 'Ser +verID' => 'xxxxx5-cfa7-4ea2-8013-34adxxxxxxx', 'Sta +tus' => 'Running', 'Ext +ernalIP' => '999.999.1.187', 'Ind +ex' => '1', 'Int +ernalIP' => '999.9.4.162', 'Sca +larizrVersion' => '2.4.5', 'Pla +tformProperties' => { + 'InstanceType' => 'm3.medium', + 'InstanceID' => 'i-e6xxxx9', + 'AMIID' => 'ami-94exxx', + 'AvailabilityZone' => 'us-west-1b' + }, 'IsD +bMaster' => '1' }, { 'Upt +ime' => '4428.4', 'Ser +verID' => 'xxxxx-625f-4afc-b544-68fe0740dc0f', 'Sta +tus' => 'Running', 'Ext +ernalIP' => '999.99.34.230', 'Ind +ex' => '2', 'Int +ernalIP' => '999.99.3.165', 'Sca +larizrVersion' => '2.4.5', 'Pla +tformProperties' => { + 'InstanceType' => 'm3.medium', + 'InstanceID' => 'i-xxx', + 'AMIID' => 'ami-xxx', + 'AvailabilityZone' => 'us-west-1b' + }, 'IsD +bMaster' => '0' } ] }, 'ID' => '68609', 'RoleID' => '61944', 'Platform' => 'ec2', 'DbMsrProperties' => { 'IsBackupR +unning' => '1395658969', 'LastBundl +eTime' => '0', 'IsBundleR +unning' => '0', 'LastBacku +pTime' => '1395475213' }, 'CloudLocation' => 'us-west-1', 'Name' => 'prod-xxx-platform-no +-sync-db-mysql64-ubuntu1204', 'Category' => 'Databases', 'PlatformProperties' => { 'Instan +ceType' => 'm3.medium', 'Availa +bilityZone' => 'us-west-1b' }, 'ScalingAlgorithmSet' => {}, 'ScalingProperties' => { 'MaxInst +ances' => '4', 'MinInst +ances' => '2' } } ] } }; print refStructure ($VAR1, 0, ' ', '$VAR1' );


Comment on Re: deeply nested hashes of arrays of hashes of...
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-09-01 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls