Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
What you need is a BigInt Compare function that allows you to tell if your potentially big integer stored in a string is bigger than Perl's max value. And if it is, then just ignore it or use the max value. You could also just check the length of the string. That would be the fastest solution. If the string is longer than, let's say, 8 bytes, then it may be a number that is too big. So, instead of using that number, you just use 99,999,999. Anything above that value gets cut off. Or you could use this:

################################################## # v2019.9.27 # Compares two large positive integers. # The integers can be binary, octal, # decimal, or hexadecimal. # # NOTE: Both numbers must be in the same base. # The numbers should not contain spaces, tabs, line breaks, # minus sign, decimal points, or anything other than digits! # Illegal characters can mess up the result. # # Returns: 0 if they are equal # 1 if the first one is greater # 2 if the second one is greater # # Special cases: # * When comparing zero against an empty string or # undefined value, the zero will be greater. # * When comparing an undefined value against # an empty string, they will be equal. # # Usage: INTEGER = CMP(STRING, STRING) # sub CMP { my $A = defined $_[0] ? uc($_[0]) : ''; my $B = defined $_[1] ? uc($_[1]) : ''; my $AL = length($A); my $BL = length($B); return 2 if ($AL < $BL); return 1 if ($AL > $BL); return 0 if ($A eq $B); # At this point, we know that both numbers have the # same length, and one of them is greater than the other. my $DIFF = 0; for (my $i = 0; $DIFF == 0 && $i < $AL; $i++) { $DIFF = vec($A, $i, 8) - vec($B, $i, 8); } return ($DIFF > 0) ? 1 : 2; }

DISCLAIMER: I am a beginner perl programmer. I wrote this sub last year, and it may have some bugs! For example, the most obvious one is that if you compare two strings "003" and "13" the result will be that the first one is greater. Why? Because it's longer. Lol :P


In reply to Re: Reliably parsing an integer by harangzsolt33
in thread Reliably parsing an integer by rdiez

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 having an uproarious good time at the Monastery: (2)
As of 2024-04-19 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found