in reply to parse string into multidimensional hash

Hello chickenman and welcome to the monastery and to the wonderful world of Perl!

since this is your first post, contrary to monks habits, I offer you a working example, but remember: the monastery is not a code writing service! So remember the golden rule: more effort you show more help you, possibly, get in return.

As you are in the need to parse a non standard format ( PS DGMGRL> SHOW CONFIGURATION; always give us as much information you can to help us to help you..;) the task is complex and the solution ugly (be brave!go learn MARPA)

use strict; use warnings; use Data::Dumper; # create top level keys.. my %hash = ( Configuration => '', 'Protection Mode' => '', Databases => {}, Properties => {}, # Fast-Start Failover => '', # it's missing in your output.. 'Configuration Status' => '' ); # track the nasty last line.. my $last_line; # track the current key with subkeys my $current_key; while (<DATA>){ chomp; # skip empty lines next if /^$/; # remove leaading spaces s/^\s+//; # split on possible separators my @fields = split /\s*\-|:|=\s/,$_; # remove unneeded spaces from fields $fields[0] =~ s/\s+$//; $fields[1] =~ s/\s//g if defined $fields[1]; if ($fields[0] eq 'Configuration Status'){next} # second field present? it is a norma key (no subkeys) if (defined $fields[1]){ $fields[1]=~s/'//g; # some debug with print... print "fields = '$fields[0]' '$fields[1]'\n"; # are we processing a subkey? if ($current_key){ print "\tdefined current key\n"; #print "current -> $current_key - $fields[0]\n"; if (exists $hash{$current_key}){ print "\texists the key '$current_key'\n"; $hash{$current_key}{$fields[0]}=$fields[1]; } } else { # exists the toplevel key? if (exists $hash{$fields[0]}){ $hash{$fields[0]} = $fields[1]; } $current_key = undef; } } # else we are processing subkeys... else{ $current_key = $fields[0]; } $last_line = $fields[0]; if (eof){ $hash{'Configuration Status'} = $last_line; } } print Dumper \%hash; __DATA__ Configuration - DGCONF Protection Mode: MaxAvailability Databases: DB1 - Primary database DB2 - Physical standby database Properties: FastStartFailoverThreshold = '30' OperationTimeout = '30' FastStartFailoverLagLimit = '30' CommunicationTimeout = '180' ObserverReconnect = '0' FastStartFailoverAutoReinstate = 'TRUE' FastStartFailoverPmyShutdown = 'TRUE' BystandersFollowRoleChange = 'ALL' ObserverOverride = 'FALSE' ExternalDestination1 = '' ExternalDestination2 = '' PrimaryLostWriteAction = 'CONTINUE' Fast-Start Failover: DISABLED Configuration Status: SUCCESS

That outputs:

fields = 'Configuration' 'DGCONF' fields = 'Protection Mode' 'MaxAvailability' fields = 'DB1' 'Primarydatabase' defined current key exists the key 'Databases' fields = 'DB2' 'Physicalstandbydatabase' defined current key exists the key 'Databases' fields = 'FastStartFailoverThreshold' '30' defined current key exists the key 'Properties' fields = 'OperationTimeout' '30' defined current key exists the key 'Properties' fields = 'FastStartFailoverLagLimit' '30' defined current key exists the key 'Properties' fields = 'CommunicationTimeout' '180' defined current key exists the key 'Properties' fields = 'ObserverReconnect' '0' defined current key exists the key 'Properties' fields = 'FastStartFailoverAutoReinstate' 'TRUE' defined current key exists the key 'Properties' fields = 'FastStartFailoverPmyShutdown' 'TRUE' defined current key exists the key 'Properties' fields = 'BystandersFollowRoleChange' 'ALL' defined current key exists the key 'Properties' fields = 'ObserverOverride' 'FALSE' defined current key exists the key 'Properties' fields = 'ExternalDestination1' '' defined current key exists the key 'Properties' fields = 'ExternalDestination2' '' defined current key exists the key 'Properties' fields = 'PrimaryLostWriteAction' 'CONTINUE' defined current key exists the key 'Properties' fields = 'Fast' 'StartFailover' defined current key exists the key 'Properties' $VAR1 = { 'Configuration Status' => 'SUCCESS', 'Configuration' => 'DGCONF', 'Protection Mode' => 'MaxAvailability', 'Properties' => { 'ObserverOverride' => 'FALSE', 'ObserverReconnect' => '0', 'PrimaryLostWriteAction' => 'CONTINUE', 'FastStartFailoverThreshold' => '30', 'CommunicationTimeout' => '180', 'ExternalDestination2' => '', 'Fast' => 'StartFailover', 'FastStartFailoverLagLimit' => '30', 'BystandersFollowRoleChange' => 'ALL', 'ExternalDestination1' => '', 'OperationTimeout' => '30', 'FastStartFailoverPmyShutdown' => 'TRUE', 'FastStartFailoverAutoReinstate' => 'TRUE' }, 'Databases' => { 'DB1' => 'Primarydatabase', 'DB2' => 'Physicalstandbydatabase' } };

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: parse string into multidimensional hash
by chickenman (Novice) on Feb 20, 2018 at 10:04 UTC
    Thank you so much for writing that piece of code. This is exactly what i was looking for. You definitely saved my day!
    Next time i'll try to be more precise.