Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Problem with Pseudohash

by uday_sagar (Scribe)
on Feb 02, 2012 at 06:24 UTC ( #951366=perlquestion: print w/ replies, xml ) Need Help??
uday_sagar has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I am having an XML document which I am parsing using XML::Simple module. Here is my xml document which is shown in 2 different styles: 1)In such a way that pseudohashh is created 2)no pseudohash

STYLE 1

<booklist> <book type="technical"> <author>Book 1 author 1</author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> </book> </booklist>

STYLE 2

<booklist> <book > <author>Book 1 author 1</author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> type="technical" </book> </booklist>

Just the position of attribute, type="technical" is changed. (thats it!)

Here is my perl code:

use strict; use XML::Simple; use Data::Dumper; my $booklist = XMLin('book.xml', ForceArray => 1); # print Dumper($booklist); foreach my $book (@{$booklist->{book}}) { print $book->{type}."\n"; }

When I give STYLE 1 to perl, its fine and displaying "technical". But this is not happening with STYLE 2. My original XML file has its contents similar to STYLE 2

What to do?

Thanks,

Uday Sagar.

Comment on Problem with Pseudohash
Select or Download Code
Re: Problem with Pseudohash
by choroba (Abbot) on Feb 02, 2012 at 08:49 UTC
    In the 2nd style, the part type="technical" is not an attribute, it is just a text. You can find it in $book->{content}, including the newline before it.
    Update: Where do you see any pseudohash?
      if the content has type1="scientific" type2="technical" instead of type="technical", how to get only type1="sceintific"?
        OK, you have a string
        my $content = "\n" . 'type1="scientific" type2="technical"';
        Are you interested in type1 only? Use
        my ($type1) = $content =~ /type1="([^"]+)"/;
        Are you interested whether scientific is present? Use
        my $is_sceintific = $content =~ /type[0-9]*="scientific"/;
Re: Problem with Pseudohash
by Anonymous Monk on Feb 02, 2012 at 08:59 UTC
    Pseudohashes aren't involved in any way shape or form

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-07-29 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls