Re: Evaluate an operator given as a string



Amit Koren wrote:
Hi all.

Hello,

I'm trying to pass the operators ">=" or "<=" to a subroutine,
and then use it inside an "if" statement.
What changes should i apply to the code below to make it work ?

my_evaluate ( ">=");

sub my_evaluate {
my ($sign) = @_;
my $x = 10;
my $y = 20;
if ($x $sign $y) { # want it to be treated as: if ($x >= $y)
...do something...
}
}

my $greater_than_or_equal_to = sub { $_[0] >= $_[1] )
my $less_than_or_equal_to = sub { $_[0] <= $_[1] )


my_evaluate( $greater_than_or_equal_to );

sub my_evaluate {
my ( $sign ) = @_;
my $x = 10;
my $y = 20;
if ( $sign->( $x, $y ) ) { # want it to be treated as: if ($x >= $y)
...do something...
}
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: Help w/code
    ... little more difficult--including sorts, pivottables, charts, subtotals.... ... Sub testme() ... Dim myRng As Range ...
    (microsoft.public.excel.programming)
  • RE: sorting range
    ... only sorts the first "key" no matter which one is first. ... "Joel" wrote: ... sub test() ... Dim myRng As Range ...
    (microsoft.public.excel.misc)
  • Re: After DataGrid Sort find Current Row?
    ... Private Sub DataGrid1_MouseDown(ByVal sender As Object, ... > but when you let up and it sorts, it doesn't show the name of the ... >> Outgoing mail is certified Virus Free. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Time entry with VBA
    ... think of that as I use it in all sorts of situations in other worksheets. ... Sub CreateMyRange() ... with worksheet functions. ... multiple ranges only. ...
    (microsoft.public.excel.misc)
  • Re: Sorting of Sorts
    ... Here's one of the quickest BASIC sorts I've seen. ... Sub HuthSortAs String) ... Dim compare As String, holdd As String ... Dim low As Long, hi As Long, md As Long ...
    (microsoft.public.vb.general.discussion)