Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file into a Hash

Status
Not open for further replies.

Haunter

Programmer
Jan 2, 2001
379
US
I have a hash stored in a text file in this format.

%userdat={key=>value,
key2=>value};

I want to open the file and read the info into another hash. I have to be able to use some other method than using 'require' or 'use'. Any ideas?
 
Why are you unable to require the file? Can you change the format of the file from being Perl qualified syntax to being just the data?
i.e.:

key:value
this:that
hibby:jibby
jabby:fribby
flibby:flabby

It would be much easier to turn the contents of a file like this into a hash.

open(FILE, "/path/to/your/file");

while(<FILE>){
my ($key, $val) = split(/:/, $_);
$your_hash{$key} = $val;
}

close(FILE);

#hash now populated.

Good luck :-Q
--Jim
 
I like Jim's solution (sorry LightElf, and Welcome, by the way) because it lets you use the data-file in different ways maybe, and it certainly doesn't hide the name of the variable in another file as it would if you required perl code. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I don't entirely agree that either solution is better than the other. There are times when one will work better, and times when the other is more appropriate. I've used both methods, depending on what I wanted to accomplish and a variety of other considerations. For somewhat sophisticated configutation files I kind of like to use the require hash method. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Quote

I want to open the file and read the info into another hash. I have to be able to use some other method than using 'require' or 'use'. Any ideas?

Unquote

Given the request, I don't see how 'require'-ing the hash is really an option. While I don't know why these constraints exist, I always like to recognize them in my reponses.

As far as storing a hash in a file in perl syntax form, only to be require'd later: why? What possible advantage could storing a hash in perl syntax possibly give? It certainly complies with laziness and impatience, but hubris? For the same reasons Mike pointed out, I wouldn't store the data as a 'syntax hash'.

I'm going to bed. Night.

--Jim
 
Thank you all for your comments.

I was using the require function to get the configuration file but I was using the eval function to test of its existance and getting odd errors that occured when the configuration was updated through another cgi script. Therefore, I sought another solution.

Placing the hash in the file in the key:dat format solved all my issues.

You Gents are wonderful.
 
Jim, as for why someone would want to create a file in perl syntax hash form only to require it later: it makes a great way to store configuration options OUTSIDE of the source code, but get them into the program quickly and conveniently. It's also handy if you have multiple configuration files (i.e. a different one for each user). You can use the username as part of the required file name. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top