interpolation question:

C

c186282

I'm reading in a file line by line. Some of the lines have variables
that I would like to replace with the current values. For example the
contents of a line that I read in may look like this:

Hi there $$info{user} what are you going to do today?

I have this line in some variable say $line. How do I replace the text
$$info{user} with the actual value of $$info{user}? Of cores $info is a
reference to a hash in the running program.



Let me explain the motivation of the problem. Maybe someone will have
a better way to attack the problem. The script that I'm writing
collects information from a database then writes an input text file for
some other program. This text file has a lot of comments and additional
fluff along with lots of key value tags. I would like to have a template
text file that my scripts reads in and just adds the current relevant
information. Also I want a clean solution I do not want to have a
large case structure of replacements.

Thank you for your help.
 
E

Eric Bohlman

I'm reading in a file line by line. Some of the lines have variables
that I would like to replace with the current values. For example the
contents of a line that I read in may look like this:

Hi there $$info{user} what are you going to do today?

I have this line in some variable say $line. How do I replace the text
$$info{user} with the actual value of $$info{user}? Of cores $info is a
reference to a hash in the running program.

See the first result returned from perldoc -q variable
Let me explain the motivation of the problem. Maybe someone will have
a better way to attack the problem. The script that I'm writing
collects information from a database then writes an input text file for
some other program. This text file has a lot of comments and additional
fluff along with lots of key value tags. I would like to have a template ^^^^^^^^
text file that my scripts reads in and just adds the current relevant
information. Also I want a clean solution I do not want to have a
large case structure of replacements.

There are an ungodly number of templating modules out there on CPAN.
Perhaps adapting one of them would make more sense in the long run than
trying to roll your own system (you'd probably get a bunch of extra
functionality for free).
 
B

Brian McCauley

c186282 wrote:

[ snip FAQ: "How can I expand variables in text strings?" ]

You are supposed to check the FAQ before you post.

But see also numerous previous thread discussing this FAQ (simply seach
for the exact string "How can I expand variables in text strings?
") because I think the answer in the FAQ is incompete.
 
C

c186282

O.K I'm embarrassed. The solution was simple and I searched with a poor
choice of words.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top