Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: making a text file to an hash array

by Ratazong (Monsignor)
on Jul 14, 2010 at 10:51 UTC ( [id://849491]=note: print w/replies, xml ) Need Help??


in reply to making a text file to an hash array

There are many ways to do so, and the solution (of course) depends on your requirements. Based on your example, try the following approach:

  1. read the text-file into one string
  2. split the string based on your delimiter (which seems to be a blank). This will give you an array
  3. Pass through the array.
    • check if the current element is a key (a single letter in your example, use =~ /\w/)
    • if yes: check if the next element is a value
      • if yes: store the value to the hash (using the key identified before)
      • if no: store an empty value to the hash (using the key identified before)
    • continue with the next element

Of course you can optimize. And probably you have to adapt the algorithm - but I hope it gives you an idea!

Rata

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found