Hi Mika
To address your code directly:
I suspect this line is the problem:
$OBU_objects[$OBU_idx] = \%OBU_hash;
Each time this line executes it stores a reference to the
same hash at a different position in @OBU_objects. To create
a new hash each time, you could to this:
$OBU_objects[$OBU_idx] = { %OBU_hash };
The { and } create an anonymous hash which is then initialised
to contain a copy of the contents of %OBU_hash. One possible
problem with this is that the contents of OBU_hash are not being
initialised each time through your loop. If every OBU always
has the same attributes, this may not be a problem.
An alternative approach might be to use 'my %OBU_hash at the
start of your outer 'if' statement. This will create a new
(empty) has each time and the original code to store a reference
to it will work (no need to add the { and }).
To take another tack altogether
Have you thought about using XML::Simple instead of XML::Parser?
While XML::Parser is a great module, it's Tree style is a real
pain to use. XML::Simple is much easier to work with as long
as your XML does not contain 'mixed content'. In the unlikely
event that you do need to work with mixed content, I'd recommend
XML::XPath instead.
Regards
Grant
=====================================================================
Grant McLean | email: [EMAIL PROTECTED] | Lvl 6, BP House
The Web Limited | WWW: www.web.co.nz | 20 Customhouse Quay
Internet Solutions | Tel: +64 4 495 8250 | Box 1195, Wellington
Awesome service | Fax: +64 4 495 8259 | New Zealand
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, May 02, 2001 10:58 PM
> To: [EMAIL PROTECTED]
> Subject: parsing XML code -> problem with HASH
>
>
> Hi,
>
> I am having a small problem while parsing the XML code. First
> of all, here
> comes snip of the sourcecode:
>
> ...
> if ( $NN->[$j] eq "OBU" ) # if OBU was found...
> {
> $OBU = $NN->[$j+1];
>
> for $e ( 0..@$OBU ) # loop through the content of OBU
> {
> if ( ref($OBU->[$e]) eq "HASH" )
> {
> $OBU_Attributes = $OBU->[$e];
> %OBU_hash = ( %OBU_hash, %$OBU_Attributes );
> }
> }
> $OBU_objects[$OBU_idx] = \%OBU_hash; # store the attributes
> $OBU_idx++; # increase the counter
> }
> ...
>
> I am using XML:Parser and tree model. As you probably might
> have figured
> out, the problem comes with the loop that checks outs either
> there is hash
> or not. There can be any number of OBU's. When I run the
> loop, every time
> the program replaces the old value with the new value. The
> result (in the
> end) is thereby the last node in the tree.
>
> So, how can I put the values from different OBU's in the own
> variables/hashes?
> Does %OBU_hash[$index_value] = ... -solution work?
>
> Additionally, here comes tree model taken with Data::Dumper:
>
> 'OBU',
> [
> {
> UID = 'FKDL',
> MID => 'FDF3',
> Version => 1,
> Type => 'FD',
> objectClass => 'OBU'
> },
>
> 'OBU',
> [
> {
> UID => 'FDF',
> MID => 'FD4F',
> Version => 1,
> Type => 'F3U',
> objectClass => 'OBU'
> },
>
> 'OBU',
> [
> {
> UID => '4T4',
> MID => 'GFG4T',
> Version => 1,
> Type => '5GF',
> objectClass => 'OBU'
> },
>
> Hope you understood.. here comes even another example:
>
> for ( $ind=0; $ind < @OBU_objects; $ind++ )
> {
> print " UID : $FUUT_objects[$ind]->{UID}\n";
> print " MID : $FUUT_objects[$ind]->{MID}\n";
> print " Version : $FUUT_objects[$ind]->{Version}\n";
> print " Type : $FUUT_objects[$ind]->{Type}\n";
> print "ObjectClass : $FUUT_objects[$ind]->{objectClass}\n";
> }
>
> when I run it, I got the exact value of the OBU_objects, but
> each of the
> looks similar.
>
> BR,
> Mika Aho
>