equality as a variable

Discussion in 'Perl Misc' started by Shiraz, Sep 30, 2005.

  1. Shiraz

    Shiraz Guest

    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
     
    Shiraz, Sep 30, 2005
    #1
    1. Advertising

  2. Shiraz

    Brian Wakem Guest

    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
     
    Brian Wakem, Sep 30, 2005
    #2
    1. Advertising

  3. Shiraz

    Shiraz Guest

    thanks a lot
     
    Shiraz, Sep 30, 2005
    #3
  4. 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
     
    A. Sinan Unur, Sep 30, 2005
    #4
  5. "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
     
    A. Sinan Unur, Sep 30, 2005
    #5
  6. Shiraz

    Anno Siegel Guest

    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.
     
    Anno Siegel, Sep 30, 2005
    #6
  7. -berlin.de (Anno Siegel) wrote in
    news:dhjufn$801$-Berlin.DE:

    > 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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 30, 2005
    #7
  8. Shiraz

    Brian Wakem Guest

    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
     
    Brian Wakem, Sep 30, 2005
    #8
  9. Shiraz

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > -berlin.de (Anno Siegel) wrote in
    > news:dhjufn$801$-Berlin.DE:
    >
    > > 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 "pissibility", 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.
     
    Anno Siegel, Sep 30, 2005
    #9
  10. Shiraz

    Anno Siegel Guest

    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.
     
    Anno Siegel, Sep 30, 2005
    #10
  11. -berlin.de (Anno Siegel) wrote in
    news:dhk4ji$b3d$-Berlin.DE:

    > A. Sinan Unur <> wrote in comp.lang.perl.misc:
    >> -berlin.de (Anno Siegel) wrote in
    >> news:dhjufn$801$-Berlin.DE:
    >>
    >> > 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.


    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. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 1, 2005
    #11
  12. Shiraz

    Babacio Guest

    "A. Sinan Unur"

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


    Bello!
    (I mean: nice solution!)

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Oct 1, 2005
    #12
  13. Brian Wakem <> wrote in news::

    > 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 :)


    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
    --
    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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 1, 2005
    #13
  14. Babacio <> wrote in news::

    > "A. Sinan Unur"
    >
    >> Hmmm ... Sorry, I am going to go with hash based solution:
    >>
    >> (...)

    >
    > Bello!
    > (I mean: nice solution!)


    Thanks, but please see Anno's post to see how to do it right.

    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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 1, 2005
    #14
  15. Shiraz

    Babacio Guest

    "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'...

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Oct 1, 2005
    #15
  16. OT: Keyboard joy (was: Re: equality as a variable)

    On Sat, 01 Oct 2005 00:02:25 +0000, A. Sinan Unur wrote:

    > 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
     
    Richard Gration, Oct 1, 2005
    #16
  17. Re: OT: Keyboard joy

    Richard Gration <> writes:

    > On Sat, 01 Oct 2005 00:02:25 +0000, A. Sinan Unur wrote:
    >
    >> 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 :)


    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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Oct 1, 2005
    #17
  18. Shiraz

    Damian James Guest

    On Fri, 30 Sep 2005 23:56:07 GMT, A. Sinan Unur said:
    > ...
    > 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
     
    Damian James, Oct 1, 2005
    #18
  19. Re: OT: Keyboard joy

    Sherm Pendley wrote:
    > Richard Gration <> writes:
    >
    >
    >>On Sat, 01 Oct 2005 00:02:25 +0000, A. Sinan Unur wrote:
    >>
    >>
    >>>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 :)

    >
    >
    > 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.

    --
    John W. Kennedy
    "Never try to take over the international economy based on a radical
    feminist agenda if you're not sure your leader isn't a transvestite."
    -- David Misch: "She-Spies", "While You Were Out"
     
    John W. Kennedy, Oct 1, 2005
    #19
  20. Re: OT: Keyboard joy

    "John W. Kennedy" <> wrote in
    news:_RC%e.1543$:

    > Sherm Pendley wrote:
    >> Richard Gration <> writes:
    >>
    >>
    >>>On Sat, 01 Oct 2005 00:02:25 +0000, A. Sinan Unur wrote:
    >>>
    >>>
    >>>>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 :)

    >>
    >>
    >> 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.


    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

    --
    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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 1, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. javaBeginner
    Replies:
    1
    Views:
    13,385
    snoopyjc
    Apr 30, 2008
  2. Karl Gorden

    Object Equality

    Karl Gorden, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    424
    Michael Borgwardt
    Oct 29, 2003
  3. jbj
    Replies:
    9
    Views:
    436
    Roedy Green
    Jul 16, 2004
  4. Toby Inkster

    Racial equality is a myth!

    Toby Inkster, Mar 10, 2005, in forum: HTML
    Replies:
    47
    Views:
    2,811
    Matt Clara
    May 24, 2005
  5. sathya_me
    Replies:
    14
    Views:
    456
    -berlin.de
    Aug 11, 2004
Loading...

Share This Page