<CCCMAP> <COMPONENT> <COMP_NAME>ip</COMP_NAME> <CCC> <CCC_NAME>ip1</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> </TC> </CCC> <CCC_NAME>ip2</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> <TC_LOC>/tftpboot/tc3</TC_LOC> <TC_LOC>/tftpboot/tc4</TC_LOC> </TC> </CCC> </COMPONENT> <COMPONENT> <COMP_NAME>parser</COMP_NAME> <CCC> <CCC_NAME>p1</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> <TC_LOC>/tftpboot/tc2</TC_LOC> </TC> </CCC> <CCC_NAME>p2</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> </TC> </CCC> <CCC_NAME>p3</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> <TC_LOC>/tftpboot/tc2</TC_LOC> <TC_LOC>/tftpboot/tc3</TC_LOC> </TC> </CCC> </COMPONENT> </CCCMAP>
open (FILE, "< $mapfile") or die ("Unable to read the file $mapfile"); while (my $str = <FILE>) { chomp($str); $comp = $1 if $str =~ m{<COMP_NAME>(.*?)</COMP_NAME>}; $cmd = $1 if $str =~ m{<CCC_NAME>(.*?)</CCC_NAME>}; $tc = $1 if $str =~ m{<TC_LOC>(.*?)</TC_LOC>}; if ($tc) { push @tc, $tc; } if ( ($str =~ m{</TC>}) ) { $hoh{$comp}{$cmd} = {@tc}; #push @{$hoh{$comp}{$cmd}}, { @tc }; next; } elsif ( ($str =~ m{</CCC>})||($str =~ m{</COMPONENT>}) ) { undef @tc; undef $tc; next; } else { next; } } # print Dumper \%hoh;
there is minor error in my code which makes this hash improper$VAR1 = { 'ip' => { 'ip1' => { '/tftpboot/tc1' => '/tftpboot/tc1' }, ' +ip2' => { '/tftpboot/tc4' => '/tftpboot/tc4', '/tftpboot/tc1' => '/tf +tpboot/tc3' } }, 'parser' => { 'p2' => { '/tftpboot/tc1' => '/tftpboo +t/tc1' }, 'p3' => { '/tftpboot/tc3' => '/tftpboot/tc3', '/tftpboot/tc +1' => '/tftpboot/tc2' }, 'p1' => { '/tftpboot/tc2' => undef, '/tftpbo +ot/tc1' => '/tftpboot/tc2' } } };
$VAR1 = { 'ip' => { 'ip1' => { '/tftpboot/tc1' }, 'ip2' => { '/tftpboo +t/tc4', '/tftpboot/tc1', '/tftpboot/tc3' } }, 'parser' => { 'p2' => { + '/tftpboot/tc1' }, 'p3' => { '/tftpboot/tc3', '/tftpboot/tc1', '/tft +pboot/tc2' }, 'p1' => { '/tftpboot/tc1' => '/tftpboot/tc2' } } };
Edit: g0n - added readmore tags
In reply to problem parsing XML into hash by rsennat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |