Newbie Question: interpretation of strings as variables 
Author Message
 Newbie Question: interpretation of strings as variables

Quote:

>I would like to have a string which is updated dynamically, and the way
>I would like to do it is to read a line from a file into a variable
>$fileline with the line in the file containing something like:
>    The value of the counter is $count
>with a statement like:
>    $fileline = <>;
>where $count is a variable in the same perl script and is updated regularly.
>So I would like to then be able to print the variable and each time it
>could print something different.
>Can anyone help please?

Here's one way:

$fileline = <>;
chop $fileline;
$fileline = 'print "'.$fileline.'\n"';
$count = 2;
eval $fileline;
++$count;
eval $fileline;

Beirne
--
-------------------------------------------------------------------------------
Beirne Konarski                 | Subscribe to the unicycling mailing list.




Sat, 15 Mar 1997 20:00:22 GMT  
 Newbie Question: interpretation of strings as variables
I would like to have a string which is updated dynamically, and the way
I would like to do it is to read a line from a file into a variable
$fileline with the line in the file containing something like:

    The value of the counter is $count

with a statement like:

    $fileline = <>;

where $count is a variable in the same perl script and is updated regularly.
So I would like to then be able to print the variable and each time it
could print something different.

Can anyone help please?

--
Lewis G Kirby   Telephone +44 473 645807
--------------------------------------------------------------------
Hindsight is the only exact science
--------------------------------------------------------------------
sig pretentious quote number 167



Sat, 15 Mar 1997 16:23:03 GMT  
 Newbie Question: interpretation of strings as variables

Quote:
>I would like to have a string which is updated dynamically, and the way
>I would like to do it is to read a line from a file into a variable
>$fileline with the line in the file containing something like:
>    The value of the counter is $count
>with a statement like:
>    $fileline = <>;
>where $count is a variable in the same perl script and is updated regularly.

If you want to replace only the string '$count' by the value of that
variable, you could do:

     ($fileline = <>) =~ s/\$count/$count/;

If you want to replace all scalar variables by their respective values:

     ($fileline = <>) =~ s/\$\w+/$&/gee;

If you're really ambitious you can do full-blown double-quote substitution:

     $fileline = eval "qq\0" . <> . "\0";

Quote:
>So I would like to then be able to print the variable and each time it
>could print something different.

The syntax above changes the value of $fileline, so that it no longer
contains any dollar signs, so doing "/\$\w+/$&/gee;" again later has no
effect.  Maybe you want to keep the original line in $fileline, and fill
in the variable in a temporary copy, like so:

        $fileline = <>;
        ($_ = $fileline) =~ s/\$\w+/$&/gee;
        print;
        :
        :
        $count = &something("else");
        :
        :
        ($_ = $fileline) =~ s/\$\w+/$&/gee;
        print;                  # uses updated values of the variables.

--
Hope this helps,

HansM



Mon, 17 Mar 1997 23:31:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Interpretation of strings as variables: summary

2. Testing if a variable is a number or a string.(Newbie question)

3. newbie question regarding s/<old string>/<new string>/g

4. HELP: variable interpretation??

5. Help Variable Interpretation ..

6. Bug with variable interpretation!?

7. Double interpretation of string w/ meta-chars?

8. Question on variable substitution in strings

9. newbe question splitting a string into two different variables

10. Question: using write to format strings in a $variable or @list

11. Expanding variables in strings questions

12. Delayed variable interpolation in string question

 

 
Powered by phpBB® Forum Software