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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading variables from file 1

Status
Not open for further replies.

kirankv123

Programmer
Nov 2, 2006
5
US
Hi,

I have then input file as follows:


input.txt file:

c:\\Data\$var1\files firstfile
c:\\Data\$var2\files secondfile


and $var1 and $var2 are exported and available through ENV.

In my Perl program, I would like to read the file and get the contents with values of $var1 & $var2 not as $var1/$var2.
export var1 = Data_Files1
export var2 = Data_Files2


But if I have the same content in the string variables then I am getting the values easily as per the expectations.

my $str1 = "c:\\Data\$var1\files"
my $str2 = "c:\\Data\$var2\files"

print $str1, $str2;

open (IN_FILE, "input.txt")

foreach $line(<IN_FILE>){
print $line; }

The above statement is displaying the same content as in file. But I am looking with the value of variables.
 
the text in your file is treated as single quoted strings so there is no variable interpolation of any meta characters such as $ and @ and \.

You have to use a regexp on the text imported from the file to interploate the two variables that are in the text: $var1, $var2.

Code:
$var1 = 'Data_Files1';
$var2 = 'Data_Files2';

open (IN_FILE, "input.txt");
while ($line = <IN_FILE>){
   $line =~ s/(\$\w+)/$1/eeg;
   print $line;
}

- Kevin, perl coder unexceptional!
 
Hi Kevin,
Could you please explain me the following line
$line =~ s/(\$\w+)/$1/eeg;

Thanks in advance
 
It searches for a '$' followed by one or more alpha-numeric characters (\w+) in the text ($line) and using some magic of the two 'e' modifiers at the end of the regexp turns that pattern into a variable and interpolates it.

From the Perl CookBook:

When Perl is compiling your program and sees a /e on a substitute, it compiles the code in the replacement block along with the rest of your program, long before the substitution actually happens. When a substitution is made, $1 is replaced with the string that matched. The code to evaluate would then be something like:

2 * 17

If we tried saying:

$text = 'I am $AGE years old'; # note single quotes
$text =~ s/(\$\w+)/$1/eg; # WRONG

assuming $text held a mention of the variable $AGE, Perl would dutifully replace $1 with $AGE and then evaluate code that looked like:

'$AGE'

which just yields us our original string back again. We need to evaluate the result again to get the value of the variable. To do that, just add another /e:

$text =~ s/(\$\w+)/$1/eeg; # finds my() variables

Yes, you can have as many /e modifiers as you'd like. Only the first one is compiled and syntax-checked with the rest of your program. This makes it work like the eval {BLOCK} construct, except that it doesn't trap exceptions. Think of it more as a do {BLOCK} instead.



- Kevin, perl coder unexceptional!
 
Which recipe is that from Kevin? Also, which version of the book?

And I assuming that you did not type all that out yourself, so do you have a pdf version or something? I'm about to update my O'Reilly collection to the latest version for a couple books, including the Cookbook. But if there is an online copy that I can purchase, that I might consider going that route instead.
 
That would be from the First edition of the Perl CookBook. I have an old copy of the Perl BookShelf References on CD (formatted in html) that I keep handy on this computer. It's recipe 1.8 in my copy. You can pick up used copies of the newest version real cheap (book or CD):


- Kevin, perl coder unexceptional!
 
Thanks. Who knew that "Expanding Variables in User Input" would be such a needed skill as to have a recipe at all, let alone that early in the cookbook?

I had to answer this question on a different forum, but I just now realized that it was the same guy. *Rolls Eyes*. At least he phrased it a lot better in the other post, as I was actually able to understand what he wanted. I laud you for being able to just understand his question, let alone have the answer so easily documented [thumbsup]
 
I had to read [reading] it a few of times before the light [idea] clicked on in my one brain cell [atom].


kirankv123 posted on another forum and I missed it? [banghead]







- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top