Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
esteemed monks, I'm overheating my brain on a function that checks if 2 values are equal, so i don't have to update a certain column in a SQL database. There are some things I think I need to take care of (like 0 vs "" vs undef, which are different in SQL). I don't have the SQL-column definition available at compare time. My 'equals' function currently looks like this:
sub equals { my ($val1, $val2) = @_; # first test for undef-ness if ( ! defined $val1 || ! defined $val2 ) { return 1 if (! defined $val1 && ! defined $val2 ); return undef; #case one is undef, other is def } # have to do next line this because strings evaluate to 0 in == co +ntext ... if ( ! $val1 && ! $val2 ) { # then test for ""-ness return 1 if ( ($val1 eq "") && ($val2 eq "") ); return undef if ( $val1 eq "" || $val1 eq "" ); #case one is "" other is 0 # then test for 0-ness return 1 if ( ($val1 == 0) && ($val2 == 0) ); } # then test for equality using 'eq' return 1 if ( $val1 eq $val2 ); # then test for equality using '==' if ( int($val1) && int($val2) ) { # all other number cases are already covered by # above tests (eg. both 0, or both the same string) # this merely takes care of cases like: # 1000.0000 == 1000 return 1 if ( $val1 == $val2 ); } # couldn't find any, so i guess they are not equal return undef; }
This looks very bloated (and a candidate for thedailywtf ), and it feels like there must be bugs lurking in this.

I'm pretty sure this can be done a lot easier/better. Anybody got a better/smarter way to accomplish this type of comparing of 2 variables?

I'm sure somebody can come up with a nicer solution, that'll make me bang my head at my desk for at least an hour.


In reply to check if 2 values are equal by eXile

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found