Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Creating a hash out of multiple XML attributes in an elment

by mirod (Canon)
on Jul 14, 2013 at 19:34 UTC ( #1044234=note: print w/ replies, xml ) Need Help??


in reply to Creating a hash out of multiple XML attributes in an elment

I think you want something like this:

my $key2value; my $t= XML::Twig->new( twig_handlers => { 'settings' => sub { file( +@_, $key2value); } } ) ->parsefile( 'sample.xml'); sub file{ my($t, $settings, $key2value)=@_; $key2value->{$settings->att( 'key')}= $settings->att( 'value'); }


Comment on Re: Creating a hash out of multiple XML attributes in an elment
Download Code
Re^2: Creating a hash out of multiple XML attributes in an elment
by perlvroom (Acolyte) on Jul 14, 2013 at 21:39 UTC
    Yep. This is going to be a stupid question but how do i retrieve/print the value for, say, key "a"?

      mirod used a key called "key", if you want a key called "a", maybe you should replace "key" with "a" or something like that, I haven't read perlintro so I'm not sure

      $key2value is a hash reference, you retrieve the value for a this way: $key2value->{a}

      see perldata for more info.

        I had tried that at first and it does print the value, but it also prints: Use of uninitialized value $key2value{"a"} in concatenation (.) or string for each "settings" element in the xml.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2015-07-05 14:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls