Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: HoAoH

by tadman (Prior)
on Aug 05, 2003 at 20:43 UTC ( [id://281157]=note: print w/replies, xml ) Need Help??


in reply to hashes, arrays, and references... oh my

Careful with your dot-star usage. This might run out of control and grab everything between the first open tag and the last close tag, regardless of all the opens and closes in the middle. Using .*? is a bit safer, but still, you should be using one of the many, many XML parsers, such as XML::DOM, XML::SAX and the like.

As for your hash problem, just reference the data in your array of hashes directly:
foreach my $entry (@{$self->{valuePair}}) { print "value=", $entry->{value}, $/; print "targetPo=", $entry->{targetPo}, $/; }
You can even index without iterating, such as $self-{valuePair}[0]{value} and so forth.

Replies are listed 'Best First'.
Re: Re: HoAoH
by regan (Sexton) on Aug 05, 2003 at 21:10 UTC
    Thanks for the advice about using parsers. I'll look into them once I've got this problem fixed. The xml is output from another program I wrote a while ago, so I understand what should be there. As for my current problem, when I run code, the Perl interpreter points to the first print line you had, and says: Not a HASH reference ... I went here with the debugger, and saw this
    x $self 0 POSwitch=HASH(0x1bc4c8) 'Id' => 'tp_incoming.vsd.10.Switch' 'Object' => 'Mailbox.gender' 'poName' => 'POSwitch' 'valuePair' => ARRAY(0x1a7b94) 0 SCALAR(0xc79d8) -> HASH(0xc7984) 'targetPo' => 'ERROR000' 'value' => 'xdefault' 1 SCALAR(0xc7a5c) -> HASH(0xc79e4) 'targetPo' => 'tp_incoming.vsd.10.Menu' 'value' => 'Mailbox.GENDER_MALE' 2 SCALAR(0xc7a8c) -> HASH(0xc7a68) 'targetPo' => 'tp_incoming.vsd.10.Menu.9' 'value' => 'Mailbox.GENDER_FEMALE'
    and
    DB<4> x $entry 0 SCALAR(0xc79d8) -> HASH(0xc7984) 'targetPo' => 'ERROR000' 'value' => 'xdefault'
    It's almost, but not quite there! ...regan
      Trying to parse XML yourself isn't only non-productive, it's boring and tedious. Creating on the fly objects, however, is fun and exciting. Why bother with boring and tedious when you jump to fun stuff?

      For example, let's run your sample XML through XML::Simple. I'll use the built-in DATA filehandle, so if you run this be sure you include it:

      use XML::Simple; use Data::Dumper; my $xml = XMLin(\*DATA); print Dumper $xml; __DATA__ <ObjectType> <AppObject>hello</AppObject> <AppObjectField>gender</AppObjectField> <valueTargetPair value="MALE" targetPo="Incoming 1" /> <valueTargetPair value="FEMALE" targetPo="Incoming 2" /> </ObjectType>
      When run on a computer that has XML::Simple installed, you should see something like:
      $VAR1 = { 'AppObject' => 'hello', 'valueTargetPair' => [ { 'value' => 'MALE', 'targetPo' => 'Incoming 1' }, { 'value' => 'FEMALE', 'targetPo' => 'Incoming 2' } ], 'AppObjectField' => 'gender' };
      Now ... let's turn it into an object:
      my $xml = XMLin(\*DATA); bless $xml, $xml->{AppObject}; warn unless ref $xml eq 'hello';
      Done. ;) But don't be fooled. $xml is still a reference to an anonymous hash reference, it just also happens to "BE A" 'hello' object. Now, let's run tadman's code on this (with a couple of modifications), but this time i won't bother blessing it, since there is no reason to for this simple example:
      my $xml = XMLin(\*DATA); foreach my $entry (@{$xml->{valueTargetPair}}) { print "value=", $entry->{value}, $/; print "targetPo=", $entry->{targetPo}, $/; }
      This prints:
      value=MALE targetPo=Incoming 1 value=FEMALE targetPo=Incoming 2
      Hope this convinces you to stick with parsers for parsing XML. The work has not only already been done, it's been tried and tested. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        I understand now, I will use a parser to parse the xml. In the meantime, I still need to solve the problem with the misbehaving references, or I'll never be able to improve my parsing. ...regan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-03-29 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found