Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comparing Two Numbers

by abs52 (Novice)
on Jan 05, 2021 at 09:36 UTC ( [id://11126357]=perlquestion: print w/replies, xml ) Need Help??

abs52 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am using version 0.77 to compare two numbers but it does not seems to be working as expected Here is the code
#!/usr/bin/env perl use version 0.77; my $wanted = '450.80.82'; my $installed = '450.57'; if (version->parse($installed) >= version->parse($wanted)) { INFO "$installed newer than or same as wanted ($wanted), not doing a +nything\n"; exit 0; }
I would expect it to not going into the if loop as 450.80.82 is great than 450.87 Have i done something wrong Thanks

Replies are listed 'Best First'.
Re: Comparing Two Numbers
by choroba (Cardinal) on Jan 05, 2021 at 09:43 UTC
    Use numify to see what's under the hood:
    print version->parse($installed)->numify, '<=>', version->parse($wante +d)->numify, "\n"; # Output: 450.570<=>450.080082

    Use 450.57.0 to get what you expect. Comparing dotted versions with different number of elements is tricky.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Thanks, unfortunately i am comparing driver version, meaning that the numbers needs to correct. As i am using the code to determine if a driver is installed and or should we be installing a new version
        What driver? If it's a Perl module, it got versioning wrong. If it's not, it might use a different versioning rules, which means you can't use Perl's version.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Hello abs52

        You could check if your two version strings both contain two dots - and append a ".0" if not (as suggested by choroba). And then use the possibly modified versions for comparision, not the original ones.

        HTH, Rata

Re: Comparing Two Numbers
by davido (Cardinal) on Jan 05, 2021 at 16:32 UTC

    Version::Compare gets it right (right for what you are expecting):

    perl -MVersion::Compare -E 'say Version::Compare::version_compare("450 +.80.82","450.57")' 1

    It's about 50 lines of code including whitespace and has no non-core dependencies at runtime. I've found it to be able to handle versions such as PHP versions, WordPress versions, and Linux-typical versions.


    Dave

      Yep. Once again the wisdom shows itself: "Check CPAN first!" No matter what it is you want to do, someone's probably already done it for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11126357]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found