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 bkrike 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 and changing variables 7

Status
Not open for further replies.

skosterow

Technical User
Feb 23, 2002
135
US
Hello guys and gals:

I looked up EVAL and search and replace to get this done but cant seem to find the answer im looking for. Its possible that i wasnt clear on what im trying to accomplish.

I have a text file, here is an example:

Text file:

$tester_name you have $no_of_questions remaining.

end of text file

This text file is read in to my perl script as @questions_remaining.

Heres what im looking for, when i print @questions_remaining, it prints to my HTML page as:

$tester_name you have $no_of_questions remaining.

What perl function could i wuse to change the variables IE $tester_name and $no_of_questions to the actual variable withing the script before printing to HTML?

Please dont tell me exactly (If at all possible) what the script would look like, rather point me in the right direction.

Thanks again!

- scott
 
wouldn't you print it out like


print &quot;$tester_name you have $no_of_questions remaining.</font>\n&quot;;

 
Since you don't want to be shown exactly how to do this, here are some hints:

For each line of input use the &quot;s&quot; operator.
On the left-hand side use a regex that will match a variable name, and capturing parens.
On the right-hand side use the eval function (or the &quot;e&quot; modifier) with the &quot;$&quot; backreference.

I learned how to do this from the &quot;c2ph&quot; program that comes
with the standard Perl distribution. Take a look at that,
if you have access to it. The author is reading command-
line arguments that have the form &quot;$var=value&quot; and actually
getting the assignments to happen in his program!
 
I use the same type of technique the mikevh mentions above in my html preprocessor for several different uses, including setting variables, evaluating conditionals in if and do statements, and even executing inline perl code! Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Heya guys -

I did it!

and thanks for all the help!

Here is the final code that WORKS!!!!! ;-)


# Get Header HTML
open (DATA_READ, &quot;<$header_html_file&quot;) or die &quot;Can not open HEADER HTML FILE at $header_html_file for read :$!&quot;;
@data_read = <DATA_READ>;
close (DATA_READ);

@header_html = @data_read; # Set HTML Header

# Substitute Data
$counter = 0;
foreach (@header_html) {
$header_html[$counter] =~ s/%TITLE%/$script_name $script_version/gi;
$counter++;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top