Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > equality as a variable

Reply
Thread Tools

equality as a variable

 
 
Shiraz
Guest
Posts: n/a
 
      09-30-2005
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

 
Reply With Quote
 
 
 
 
Brian Wakem
Guest
Posts: n/a
 
      09-30-2005
Shiraz wrote:

> 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")) {
....
}



--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
 
 
 
Shiraz
Guest
Posts: n/a
 
      09-30-2005
thanks a lot

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-30-2005
Brian Wakem <> wrote in news::

> Shiraz wrote:
>
>> 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")) {
> ...
> }
>


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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-30-2005
"A. Sinan Unur" <> wrote in
news:Xns96E184A1ED780asu1cornelledu@132.236.56.8:

> return 0 + $handler->($v1, $v2);


return $handler->($v1, $v2);

> print "$x: ", do_compare_op(@{ $examples{$x} }), "\n";


print "$x: ", 0 + do_compare_op(@{ $examples{$x} }), "\n";

Sinan
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-30-2005
A. Sinan Unur <> wrote in comp.lang.perl.misc:
> Brian Wakem <> wrote in news::
>
> > Shiraz wrote:
> >
> >> 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")) {
> > ...
> > }
> >

>
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-30-2005
(Anno Siegel) wrote in
news:dhjufn$801$:

> A. Sinan Unur <> wrote in comp.lang.perl.misc:
>> Brian Wakem <> wrote in
>> news::
>>
>> > 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 )

Thank you.

Sinan.

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      09-30-2005
A. Sinan Unur wrote:

> 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



--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-30-2005
A. Sinan Unur <> wrote in comp.lang.perl.misc:
> (Anno Siegel) wrote in
> news:dhjufn$801$:
>
> > A. Sinan Unur <> wrote in comp.lang.perl.misc:
> >> Brian Wakem <> wrote in
> >> news::
> >>
> >> > 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 "****ibility", was tempted to let it stand
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-30-2005
Brian Wakem <> wrote in comp.lang.perl.misc:
> A. Sinan Unur wrote:
>
> > 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


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

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Java code for determining binary file equality javaBeginner Java 1 04-30-2008 12:33 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Checking the type of a variable with equality operator sathya_me C Programming 14 08-11-2004 07:35 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
Object Equality Karl Gorden Java 5 10-29-2003 05:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57