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

Variable substitution ??? 2

Status
Not open for further replies.

drathbone

Technical User
Jun 11, 2001
39
0
0
GB
I'm sure there's an easy way to do this....

I have a variable, lets call it $var1..
$var1 = &quot;$var2 < 50&quot;. Another variable called $var 2...
$var2 = 25....

Is there anyway to do a line like,
if $var1 then ......
so that it substitues for the contents of $var1 and does something like if $var2 < 50 then .....

I'm parsing a load of text which includes a lot of conditional statements. I know I could write a long-winded evaluation, I just wondered if there was a simpler way to substitute the CONTENTS of a variable into an 'if' statement.

Any help would be MUCH appreciated.

Thanks
Darren Rathbone
 
|| is a logical OR
&& is logical AND


so:

my $var1 = 1;
my $var2 = 24;
if (($var1 == &quot;1&quot;) && ($var2 > &quot;2&quot;)){print &quot;YEP we did it\n&quot;;}
else {print &quot;Nopey Dopey sorry\n&quot;;}

HTH
 
Sorry, that dosen't quite help me....
Maybe a better explanation....

My actual variable $var1 contains the following string &quot;endurance_percent < 50&quot;.

$var1 = &quot;endurance_percent < 50&quot;;

$var1 is grabbed from a load of text, and there are many others I need to evaluate. What I need to do is evaluate the contents of $var1, based on other variables in my program. so....

$var1 = &quot;endurance_percent < 50&quot;;
$endurance_percent = 45;

I need to do the equivalent of :

if ($endurance_percent < 50) {.....

based on the contents of $var1....



 
I think I misunderstood your question. Can you ask your question again, maybe just reword it?
 
So what you need then is :

if ($endurance_percent < 50){ #its less}
elsif ($endurance_percent > 50){#its greater}
else {#clueless}


Is that what you thinking?
Oh, you posted while I was asking my misunderstood above question?
 
Okay ;-)

I need to parse a load of text like the following:

1) 4/5/5
2) if endurance_percent < 50 then 10/5/5
4) if endurance_percent < 40 then 4/8/8
10) if score = 3 then 1/1/18

Parsing the text is fine (it's the language for an online game). The above text is supplied by the user and interpretted line-by-line by my script. My script needs to evaluate each line and, if the line is true (i.e. if the endurance_percent value IS less than 50) then set another variable to equal 10/5/5 !!

So, I end up with a variable, let's call it $var1, which contains the following text &quot;endurance_percent < 50&quot;. I need to evaluate that statement against a variable within my script (called $endurance_percent, which has an integer value). So I need to do something along the lines of :

if ($endurance_percent < 50) { ...... }

But the &quot;endurance_percent < 50&quot; bit is contained in the string $var1. I need to find out how to get an 'if' statement to evaluate the contents of a string, such as if ($var1) {.....} <--- I know that this isn't what I need ;-)

Thanks again...
Darren
 
thats right, but the endurance_percent < 50 bit is ONLY known in the variable $var1....
 
SO your who problem is setting a varible then, right???

my $var = &quot;mike&quot;;
my $var1 = &quot;drathbone&quot;;
my $var2 = &quot;apple&quot;;
my $var3 = &quot;grapes&quot;;

if ($var eq &quot;mike){
#set a new global var
$new_var = &quot;global&quot;;
} elsif ($var1 eq &quot;drathbone&quot;){
#set a new global var
my $new_var = &quot;globally&quot;;
} elsif ($var2 eq &quot;apple&quot;){
#set a new global var
my $new_var = &quot;globallllly&quot;;
} elsif ($var3 eq &quot;grapes&quot;){
#set a new global var
my $new_var &quot;globalllllllly&quot;;
} else {
#we are f**ked
}
 
:) Nope... sorry....
I need to execute the following line :

if (xxx) { ..... }

Where xxx is the contents of a variable, and that variable contains an expression.

so instead of typing :

if ($x = 1) { ..... }

I need a way of executing :

$var1 = &quot;$x = 1&quot;;
if (contents of $var1 are correct) { ..... }

Hopefully thats a better explanation of what I need.

Thanks again for your time and help.
Darren

 
Sorry I don't know ow else to help you but what I have already said. The only way I know to check if a var contains BLAH is an if statement.

$var = &quot;$x = 1&quot;;
if ($var eq [&quot;$x = 1&quot;]){print &quot;It is what u want\n&quot;;}
else {print &quot;It is not\n&quot;;}
 
I think you are in need of an 'eval'.

For the perl docs, see
>perldoc -f eval -enter-

'eval' treats it's argument as a small program. It runs that program and returns the results.

Code:
#!/usr/local/bin/perl
$endurance_percent = '55';

if (eval($endurance_percent < 50)) 
    {
    print &quot;weakling\n&quot;;
	}
else {
	print &quot;Wow, you're strong\n&quot;;
	}
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
or in this particular context, where it's in a string var

$var1 = '$endurance_percent < 50';
if (eval($var1))
{
print &quot;weakling\n&quot;;
}
else {
print &quot;Wow, you're strong\n&quot;;
}
Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
:)

Thanks a million !!!
You can both be proud in the fact that I got an email from perlhelp.com this morning, suggesting a solution. Whilst their solution did work, it split the $variable up and eval'd each one in turn.
That's solved my problem.

Again, thanks,
Darren

 
FYI :
The solution mailed to me from perlhelp.com was as follows :


#!/usr/bin/perl

# NOTE: don't use strict

my $variable = 'endurance';
my $condition = '>';
my $value = '70';

$endurance = 0; # this variable can not be declared with 'my'
# because my variables are not in the symbol table.

if (eval(&quot;${$variable} $condition $value&quot;)) {
print(&quot;True\n&quot;);
}
else {
print(&quot;False\n&quot;);
}

$condition = '<';
if (eval(&quot;${$variable} $condition $value&quot;)) {
print(&quot;True\n&quot;);
}
else {
print(&quot;False\n&quot;);
}


As you can see, nice code but a bit long-winded ;-)
The solution posted above by Mike Lacey was the exact code I used in my script. Thanks again !!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top