in reply to Re: Again on "can't use string as hash as HASH ref while "strict refs" in use"
in thread Again on "can't use string as hash as HASH ref while "strict refs" in use"
This demo shows how with XML simple your structure can change depending on how many devices you have in the xml.
poj#!perl use strict; use warnings; use XML::Simple; use Data::Dump 'pp'; my @xml=(); # 2 devices $xml[0] = ' <xml> <cfg> <device name="switch1" ip="192.168.0.1" localfile="filename1"/> <device name="switch2" ip="192.168.0.2" localfile="filename2"/> </cfg> </xml>'; # 1 device $xml[1] = ' <xml> <cfg> <device name="switch1" ip="192.168.0.1" localfile="filename1"/> </cfg> </xml>'; # run for each xml for my $i (0..1){ my $heap = XMLin($xml[$i]); for my $name (keys %{$heap->{cfg}->{device}}){ push @{ $heap->{cfg}->{devices}},$name; } print pp $heap; print "\n-----------------------\n"; foreach my $name (@{ $heap->{cfg}->{devices} }) { my $CFG = $heap->{cfg}->{device}->{$name}->{localfile}; print "$CFG\n"; } print "-----------------------\n"; }
|
|---|