Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Multidimensional Hash losts entrys

by choroba (Cardinal)
on Aug 13, 2014 at 13:26 UTC ( [id://1097283]=note: print w/replies, xml ) Need Help??


in reply to Multidimensional Hash losts entrys

You're overwriting the hash in every iteration. You can assign to a hash slice, fortunately:
@complete{@arr_a} = ...;

It's not clear what exactly you want to assign, though. Does the following work for you?

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use XML::LibXML; my $xml = 'XML::LibXML'->load_xml( string => <<'__XML__'); <root> <node a="a b" b="A B" c="AA BB"/> <node a="1" b="2" c="3"/> <node a="X Y Z" b="x y z" c="xx yy zz"/> </root> __XML__ my %complete; for my $node ($xml->documentElement->findnodes('*')) { my ($a, $b, $c) = map $node->getAttribute($_), 'a', 'b', 'c'; if (3 == grep defined, $a, $b, $c) { my @arr_a = split (/ /, $a); my @arr_b = split (/ /, $b); my @arr_c = split (/ /, $c); @complete{@arr_a} = map [ shift @arr_b, shift @arr_c ], @arr_a +; } else { print "Warning! \n"; } } print Dumper \%complete;

Update: Note that I populate the hash only if all the attributes are defined. In your case, the @arr_X arrays were global, so the hash was repopulated again with the old values. (I use strict and warnings, why don't you?)

The map expression might look a bit complex for an untrained eye. It just picks one element from b and c for each element of a.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Multidimensional Hash losts entrys
by Anonymous Monk on Aug 14, 2014 at 05:23 UTC
    Thank you a lot ! Your solution helped me to solve this Problem. Now I understand, why it never worked for me. Also thank your for showing me the right way to work further. Best regards!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found