equality as a variable

S

Shiraz

i need to put the equality in an if statement as a variable

($val1, $val2, $comp) = (1,2,"gt");
if ($val1 $comp $val2)
{ print $val1; }
i couldnt find anything in the achives.... if some knows it, please let
me know.. or point me in a direction i can research the method.
thanks
 
B

Brian Wakem

Shiraz said:
i need to put the equality in an if statement as a variable

($val1, $val2, $comp) = (1,2,"gt");
if ($val1 $comp $val2)
{ print $val1; }
i couldnt find anything in the achives.... if some knows it, please let
me know.. or point me in a direction i can research the method.
thanks


if (eval("$val1 $comp $val2")) {
....
}
 
A

A. Sinan Unur

Brian Wakem said:
if (eval("$val1 $comp $val2")) {
...
}

Hmmm ... Sorry, I am going to go with hash based solution:

#!/usr/bin/perl

use strict;
use warnings;

use Carp;

my %handlers = (
'>' => sub { $_[0] > $_[1] },
'<' => sub { $_[0] < $_[1] },
'>=' => sub { $_[0] >= $_[1] },
'<=' => sub { $_[0] <= $_[1] },
'==' => sub { $_[0] == $_[1] },
'!=' => sub { $_[0] != $_[1] },
'<=>' => sub { $_[0] <=> $_[1] },
);

sub do_compare_op {
my ($v1, $op, $v2) = @_;
my $handler = $handlers{$op};
croak "Undefined op: $op" unless defined $handler;
return 0 + $handler->($v1, $v2);
}

my %examples = (
'5 == 6' => [ 5, '==', 6 ],
'5 <= 6' => [ 5, '<=', 6 ],
'5 <=> 6' => [ 5, '<=>', 6 ],
);

for my $x (keys %examples) {
print "$x: ", do_compare_op(@{ $examples{$x} }), "\n";
}

__END__

C:\Documents and Settings\asu1\My Documents> perl t.pl
5 == 6: 0
5 <=> 6: -1
5 <= 6: 1
 
A

Anno Siegel

A. Sinan Unur said:
Brian Wakem said:
if (eval("$val1 $comp $val2")) {
...
}

Hmmm ... Sorry, I am going to go with hash based solution:

#!/usr/bin/perl

use strict;
use warnings;

use Carp;

my %handlers = (
'>' => sub { $_[0] > $_[1] },
'<' => sub { $_[0] < $_[1] },
'>=' => sub { $_[0] >= $_[1] },
'<=' => sub { $_[0] <= $_[1] },
'==' => sub { $_[0] == $_[1] },
'!=' => sub { $_[0] != $_[1] },
'<=>' => sub { $_[0] <=> $_[1] },
);

I'd use the hash *and* eval:

my %handlers = map { $_ => eval "sub { \$_[0] $_ \$_[ 1] }" }
qw( > < >= <= == != <=>),
# qw( gt lt ge le eq ne cmp),
;

That string-eval is perfectly safe, everything comes from inside the source.

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
....
Hmmm ... Sorry, I am going to go with hash based solution:
....

I'd use the hash *and* eval:

my %handlers = map { $_ => eval "sub { \$_[0] $_ \$_[ 1] }" }
qw( > < >= <= == != <=>),
# qw( gt lt ge le eq ne cmp),
;

That string-eval is perfectly safe, everything comes from inside the
source.

I always type too much :))

Thank you.

Sinan.
 
B

Brian Wakem

A. Sinan Unur said:
Hmmm ... Sorry, I am going to go with hash based solution:


Hey no need to apologise to me, it's your keyboard you should be saying
sorry too for all those extra keystrokes :)
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in
A. Sinan Unur said:
Shiraz wrote:

i need to put the equality in an if statement as a variable
...
Hmmm ... Sorry, I am going to go with hash based solution:
...

I'd use the hash *and* eval:

my %handlers = map { $_ => eval "sub { \$_[0] $_ \$_[ 1] }" }
qw( > < >= <= == != <=>),
# qw( gt lt ge le eq ne cmp),
;

That string-eval is perfectly safe, everything comes from inside the
source.

I always type too much :))

It isn't just the laziness aspect, in fact the fewer lines of code may
take longer to write (and debug) than straight-forward typing of the
obvious. There is also reliability.

Basically, in the explicit definition of the hash you specify the same
thing twice which implies a possibility[1] of getting it wrong. Even
if the probability is minute, if you do this (programming) all day, it
is bound to happen once in a while, especially in maintenance. You're
safe from that when you specify each operator only once. The cost is
a possible loss in clarity.

Later you have more duplicate specifications:

# from memory
my %examples = (
'5 == 6' => [ 5, '==', 6],
'5 <= 6' => [ 5, '<=', 6],
# ...
);

In a similar vein, I wouldn't even use a hash for that, but specify a list
of strings:

my @examples = (
'5 == 5',
'5 <= 6',
'5 <=> 6',
'"E" eq "F"',
'"E" cmp "F"',
);

and run them as

print "$_: ", do_compare_op( split), "\n" for @examples;

with do_compare_op unchanged. That also shows them in the sequence
they are specified.

Anno

[1] initially mistyped "pissibility", was tempted to let it stand
 
A

Anno Siegel

Brian Wakem said:
Hey no need to apologise to me, it's your keyboard you should be saying
sorry too for all those extra keystrokes :)

If you run your eval-solution in a hostile environment, it may be
your files who are due an apology.

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in
comp.lang.perl.misc:

Shiraz wrote:

i need to put the equality in an if statement as a variable ...

Hmmm ... Sorry, I am going to go with hash based solution:
...

I'd use the hash *and* eval:

my %handlers = map { $_ => eval "sub { \$_[0] $_ \$_[ 1] }" }
qw( > < >= <= == != <=>),
# qw( gt lt ge le eq ne cmp),
;

That string-eval is perfectly safe, everything comes from inside
the source.

I always type too much :))

It isn't just the laziness aspect, in fact the fewer lines of code may
take longer to write (and debug) than straight-forward typing of the
obvious. There is also reliability.

Basically, in the explicit definition of the hash you specify the same
thing twice which implies a possibility[1] of getting it wrong. Even
if the probability is minute, if you do this (programming) all day, it
is bound to happen once in a while, especially in maintenance.

Just to re-enforce your point: I did actually mistype <= as =< in the
definition of %handlers when I first put together the example. Lucky for
me, perl caught that, so I fixed it before posting the code, but your
point is taken.

Thank you.

Sinan
 
A

A. Sinan Unur

Brian Wakem said:
Hey no need to apologise to me, it's your keyboard you should be saying
sorry too for all those extra keystrokes :)

I have an old IBM AT keyboard whose keys have that wonderful clicking
sounds which are audible pretty much all over my end of the hallway. I
think I should be apologizing to my co-workers.

In any case, please note the difference between your string eval and
Anno's string eval.

Sinan
 
B

Babacio

"A. Sinan Unur"
Thanks, but please see Anno's post to see how to do it right.

Yes, I kept 'zitto' (quiet) to avoid being noisy, but indeed...
nevertheless, I still like your solution. It's more like 'natural
parsing'...
 
R

Richard Gration

I have an old IBM AT keyboard whose keys have that wonderful clicking
sound

Jealousy is not an emotion I indulge if I can help it, but ... I'll make
an exception here :)

--
On two occasions I have been asked [by members of Parliament!], "Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?" I am not able rightly to apprehend the kind of
confusion of ideas that could provoke such a question.
-- Charles Babbage
 
S

Sherm Pendley

Richard Gration said:
Jealousy is not an emotion I indulge if I can help it, but ... I'll make
an exception here :)

Amen to that. Those are the second-best keyboards ever made. The *best*, of
course, were also made by IBM, and attached to 3278 terminals. I've often
wonder if it would be feasible to hack a USB adapter onto one of those...

sherm--
 
D

Damian James

...
Just to re-enforce your point: I did actually mistype <= as =< in the
definition of %handlers when I first put together the example. Lucky for
me, perl caught that, so I fixed it before posting the code, but your
point is taken.

The one to watch out for is => for >=. At least =< is a syntax
error :)

--Damian
 
J

John W. Kennedy

Sherm said:
Amen to that. Those are the second-best keyboards ever made. The *best*, of
course, were also made by IBM, and attached to 3278 terminals. I've often
wonder if it would be feasible to hack a USB adapter onto one of those...

Actually, the 2260 keyboard was better; when the application wasn't
accepting strokes, the keys physically locked.
 
A

A. Sinan Unur

Actually, the 2260 keyboard was better; when the application wasn't
accepting strokes, the keys physically locked.

Is the 3278 the one that actually had the APL symbols on the keys with,
gosh, I don't know, something like 96 functions keys? If so, I used that
one for about a year, and loved it (not APL, the keyboard :)

I don't think I have ever heard of the 2260.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top