Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Map multiple xml tags to array

by Anonymous Monk
on Aug 14, 2014 at 11:12 UTC ( [id://1097409]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I'm trying to map a list of tags in xml into an array in perl. At the moment i have:

XML:

<Players> <PlayerA>Name_A</PlayerA> <PlayerB>Name_B</PlayerB> ... <PlayerN>Name_N</PlayerN> <Players>

Perl:

use XML::Simple; use Text::Iconv; my $iconv; my $xmlconf = XMLin("players.xml"); my $players = $iconv->convert($xmlconf->{Players}); my $player = 'J'; my @players_list = \$players; foreach my $play (@players_list) { if($play eq $player) { print "Player found"; last; } }

However, I'm getting an error saying that Hash cannot be used a array while strict refs are in use.

Is there any other way to convert the map of hash to array?

Thanks

Replies are listed 'Best First'.
Re: Map multiple xml tags to array
by AppleFritter (Vicar) on Aug 14, 2014 at 11:30 UTC

    Howdy, dear anonymous monk,

    Can't call method "convert" on an undefined value at 1097409.pl line 1 +0.

    could you be so kind and actually post working code that shows the problem you're having? The monks are more than happy to help you, but we'd rather not fix up unrelated issues with your code first.

    In the same vein, the XML snippet you posted is not well-formed (the last line should be </Players> instead).

    That said, here's some tips for solving your problem: Be sure to read the documentation for XML::Simple and familiarize yourself with the kind of data structures it returns. Use Data::Dumper or a similar module to get an idea of what the data you're getting for YOUR file looks like. And finally, keys is your friend when iterating over hashes.

      Hi, i don't understand, my working code is exactly as i place here, and i don't get the undefined error.

        in addition to what AppleFritter and GotToBTru noted,
        you report about
        an error saying that Hash cannot be used a array while strict refs are in use.
        The posted code doesn't even have strict in use...

        You declare the variable $iconv, and in the next mention of it, call its convert method, without ever defining that $iconv is any kind of object (Text::Iconv->new).

        1 Peter 4:10
Re: Map multiple xml tags to array
by scorpio17 (Canon) on Aug 14, 2014 at 13:46 UTC

    I think this line is your problem:

    my @players_list = \$players;

    If $players is a hashref, and you want to build an array of hashrefs, you should do something like this:

    push(@players_list, $players);

    If you want to convert a hashref into an array, you would do something like this:

    @players_list = @{ %$players };

    Note that the hash "names" will be at indexes 0,2,4,6... and the hash "values" will be indexes 1,3,5,7...

    I hope that helps!

Re: Map multiple xml tags to array
by Monk::Thomas (Friar) on Aug 14, 2014 at 12:14 UTC

    What AppleFritter said. Additionally:

    STATUS OF THIS MODULE

    The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended.

    The major problems with this module are the large number of options and the arbitrary ways in which these options interact - often with unexpected results.

      monks, is there a different module with a "simple" interface like XML::Simple? having trouble finding one

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1097409]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found