Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have some alumni data from which I need to extract useful info. To that end I'm trying to go from the native (pseudo)XML file to a key/value hash.
Before stripping out the excess XML tags the @info array has the full record of one person, a sample of which is in Listing 1. Listing 2 is printed by the code segment shown and the error message is below that.
I've preloaded %data hash just to be sure I can see if there are any changes in the hash.
I'd like to know what the error means, but the real question is how to transform an array into a hash.
The nature of this data is that the keys will all be unique.
(code being tested on a Win98 PC using Active State version 5.005_03)

########### Code Segment ############## my %data = (one => 1, two => 2, three => 3); : # XML stripped off here : print @info; # Prints Listing 2 print %data; # bottom of listing 2 %data = @info =~ /(\w+)\s*(\w+)/g; # Line that gives error print %data; # Prints nothing ####################################### ### Listing 1 ### <Position> Stdnt</Position> <GradYear> 1987</GradYear> <WebPage> http://</WebPage> <E-mail> SomeAddr@abc.com</E-mail> <Comments> Needs highschool yearbooks</Comments> ### Listing 2 ### Position Stdnt GradYear 1987 WebPage http:// Email SomeAddr@abc.com Comments Needs highschool yearbooks three3two2one1 ### Error message ### Applying pattern match to @array will act on scalar(@array) at e.pl li +ne 29

Due to the embedded spaces in some of the data, a better approach might be to strip the XML and create the hash in the same step, but not till I can get through this problem.

Update: Thank you Zaxo, saintmike and ysth. I'm still recovering from the first bout of flu I've had in 20 years and not thinking clearly. I'll put this to use in a few days. Again, thanks!

-Theo-      (so many nodes and so little time ... )


In reply to Putting an array into a hash by Theo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found