From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ukhas jean Sent: 27 September 2006 07:24 To: Active Perl Subject: comapring versions
> Hi, > > I had to compare two version nos. eg. 2.9.10.2 and 10.0.0.1 > how do i do it in perl?? I was unable to find a function for it ... the only way i can think is using if-loops > ... is there a better way??? Where did you look? If you had tried searching CPAN (http://search.cpan.org for, say, "compare version", one of the results would have been "version", which is described as "Perl extension for Version Objects". It seems to be in the ppm archive, so it should be easy to install and try out. This is probably the best way. The second best way might be to DIY as you suggest, but I don't know what you mean by if-loops (while loops, for loops and foreach loops I have heard of). For example (with no ifs): -------------------------------------------------- use strict; use warnings; # Assumes both versions have the same number of numeric # elements. Returns the same as the cmp and <=> operators. sub compare_versions { my @v1 = split /\./, shift; my @v2 = split /\./, shift; my $result = 0; while (@v1 > 0 and $result == 0) { $result = shift(@v1) <=> shift(@v2); } return $result; } my @str = ("equal to", "greater than", "less than"); while (<DATA>) { chomp; my ($v1, $v2) = split; print "$v1 is ", $str[compare_versions($v1, $v2)], " $v2\n"; } __DATA__ 2.9.10.2 10.0.0.1 10.0.0.1 2.9.10.2 2.9.10.2 2.9.10.2 10.0.0.1 10.0.0.1 -------------------------------------------------- HTH -- Brian Raven ================================= Atos Euronext Market Solutions Disclaimer ================================= The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful. If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of Atos Euronext Market Solutions. L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bien vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre systeme. Le contenu de ce message electronique ne represente pas necessairement la position ou le point de vue d'Atos Euronext Market Solutions. _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
