in reply to problem parsing XML into hash

Well as said above you really ought to parse this a pure xml.

You also don't say what's wrong with the output, which makes is hard to guess what the correct out put should be.

Anyway, if you're intent on hacking this, the following code give the output as described. Is this what you're after?

(PS. Could you change the title to something more searchable like 'problem parsing xml into a hash?')

while (my $str = <DATA>) { chomp($str); $comp = $1 if $str =~ m{<COMP_NAME>(.*?)</COMP_NAME>}; $cmd = $1 if $str =~ m{<CCC_NAME>(.*?)</CCC_NAME>}; if ($str =~ m{<TC_LOC>(.*?)</TC_LOC>}) { $hoh{$comp}{$cmd}{$1} = undef; } } print Dumper \%hoh; __OUTPUT__ $VAR1 = { 'ip' => { 'ip1' => { '/tftpboot/tc1' => undef }, 'ip2' => { '/tftpboot/tc3' => undef, '/tftpboot/tc4' => undef, '/tftpboot/tc1' => undef } }, 'parser' => { 'p2' => { '/tftpboot/tc1' => undef }, 'p3' => { '/tftpboot/tc2' => undef, '/tftpboot/tc3' => undef, '/tftpboot/tc1' => undef }, 'p1' => { '/tftpboot/tc2' => undef, '/tftpboot/tc1' => undef } } };
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: error in my code
by rsennat (Beadle) on Nov 23, 2005 at 12:00 UTC
    thats almost the one what i expect.
    but please see this format,
    $VAR1 = { 'ip' => { 'ip1' => { '/tftpboot/tc1' }, 'ip2' => { '/tftpboot/tc3', '/tftpboot/tc4', '/tftpboot/tc1' } }, 'parser' => { 'p2' => { '/tftpboot/tc1' }, 'p3' => { '/tftpboot/tc2', '/tftpboot/tc3', '/tftpboot/tc1' }, 'p1' => { '/tftpboot/tc2', '/tftpboot/tc1' } } };
    thanks
      That's not a valid data structure. This is what [id://Perl Mouse] was saying. You've got an odd number of elements in your hash (under 'ip2' and 'p2', infact all of them except 'p1')

      UPDATE:

      Swapping part of the hash for an array gives you this... Is this any closer?

      while (my $str = <DATA>) { chomp($str); $comp = $1 if $str =~ m{<COMP_NAME>(.*?)</COMP_NAME>}; $cmd = $1 if $str =~ m{<CCC_NAME>(.*?)</CCC_NAME>}; if ($str =~ m{<TC_LOC>(.*?)</TC_LOC>}) { push(@{$hoh{$comp}{$cmd}}, $1); } } __OUTPUT__ $VAR1 = { 'ip' => { 'ip1' => [ '/tftpboot/tc1' ], 'ip2' => [ '/tftpboot/tc1', '/tftpboot/tc3', '/tftpboot/tc4' ] }, 'parser' => { 'p2' => [ '/tftpboot/tc1' ], 'p3' => [ '/tftpboot/tc1', '/tftpboot/tc2', '/tftpboot/tc3' ], 'p1' => [ '/tftpboot/tc1', '/tftpboot/tc2' ] } };
      ---
      my name's not Keith, and I'm not reasonable.
        Thanks a lot

        I understand that the previous one is not a proper hash, because of having a array in { }, curly braces.

        thanks
        rsennat