convert read data to variable

I have a text file called file1.txt. I want to be able to read in a line of that file (let's say the line is "score=2"). I now want to be able to use that as a variable and value in my current program.

In other words, I now want to have a variable called "$score" in my current program and have it's value set to "2" based on the data I read in from the text file.

I don't know what I will be reading in from the text file but I do know that it will always be [variable]=[value] so I can't predetermine the variable name because I don't know what it will be until I read it from the text file.

Is this possible? and if so how do I do it?

Thanks.
LVL 1
johnsmAsked:
Who is Participating?
 
ozoCommented:
while( $data=<DATAFILE> ){
    $data =~ /(\w+)=(.*)/;
    $$1 = $2;
}
0
 
ozoCommented:
/(\w+)=(.*)/;
$$1 = $2;
0
 
johnsmAuthor Commented:
What exactly does that do? I can't seem to use it properly, it just halts my execution with no errors.

How would it fit in here?
<pre>

while ($data=<DATAFILE>) {
  /(\w+)=(.*)/;
  $$1 = $2;
}
</pre>

{sorry if the tags show up.}





0
 
johnsmAuthor Commented:
ah, I see. I'm not sure how or why it works, but it does, thanks.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.