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

scalar as operator 1

Status
Not open for further replies.
Sep 29, 2003
29
GB
I want to use a scalar, ie a string contents as an operator, but am unsure how to define it. Best explained by example.

$examplecomparison = "> 0.5";
$comparision = 0.3

what I want to say is;

if ( $comparision > 0.5 )
{ print "exception created \n"; };

but I have a large number of operators and thresholds, so I want to store these in an array, that I can simply reference the comparision, something like;

if ( $comparision $examplecomparision )
{ print "exception created \n"; };

now this does not work, as I want the actual string contents to be used here.

Any idea on how to implement ?

Ta.
 
Hi there. I am a newbie myself but I have learnt that the best way to help yourself is to try and help others...

Code:
if ( $comparision $examplecomparision )
Is there a typo in there?
Because IF is a conditional statement, do you mean IF something equals? something.
 
no typo, see the if statement about it. Its how to express it is my question. I am not saying the second statement is correct ( in fact I know its not ), but how do I get the contents of the string to be parsed rather than a string reference. There might be other ways, ie eval ?
 
ok the answer is;

$examplecomparison = "> 0.5";
$comparision = 0.3

if ( eval "$comparision $examplecomparision)
{ print "exception created \n"; };

( I went experimenting ).
 
one possibility:

Code:
my $examplecomparison = "> 0.5";
my $comparison = 0.3;
my $x = qq~if ($comparison $examplecomparison) {print "exception created\n"} else {print "No Exception\n"}~;
eval $x;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top