Training, Open Source computer languages

This is page http://www.wellho.net/forum/Perl-Programming/Finding- ... umber.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Finding the latest version number

Posted by smithce (smithce), 3 June 2005
Hi

I am need of some inpiration/guidance.

I have the results of a query (of objects from our code control database) and need to identify the latest version of an object.
The problem is that the object version could be an integer, or (if the object has been 'branched') of the format n.n.n.n.

Here are some examples below:

ACT_VAL_MOVEMENTS_SUMMARY.prc-3:sql:1
BOB_BENEFIT_COMPONENT.vw-16:ascii:2
BOB_ESCALATION_BASE.vw-10:ascii:2
BOB_ESCALATION_BASE.vw-11:ascii:2
BOB_GMP_COMPONENT.vw-17:ascii:2
BOB_PR_COMPONENT.vw-12:ascii:2
BasePkgs.sql-4.1.6:sql:2
BasePkgs.sql-4.1.7:sql:2
BasePkgs.sql-4.1.8:sql:2
F_GET_ANNUAL_AMT_PENCE.fnc-3:sql:1
LifeJoin.sql-4:sql:5
LifeJoin.sql-5:sql:5
PLA_error_summary.sh-8:shsrc:1
UAPS.pkg-3.1.27:ascii:1
UAPS_BENEFITCOMPONENT.pkg-10.1.11:ascii:1
UAPS_BENEFITCOMPONENT.pkg-11:ascii:1
UAPS_BENEFITCOMPONENT.pkg-12:ascii:1
UAPS_CONTRACTEDBENEFIT.pkg-13.1.14:ascii:1
UAPS_CONTRACTEDBENEFIT.pkg-13.1.15:ascii:1
UAPS_ESCALATIONBASE.pkg-10:ascii:1
UAPS_ESCALATIONBASE.pkg-11:ascii:1
UAPS_ESCALATIONBASE.pkg-12:ascii:1
UAPS_GMPCOMPONENT.pkg-13:ascii:1
VAL_EXTRACT.prc-43:sql:2
VAL_EXTRACT.prc-44:sql:2
VAL_EXTRACT.prc-45:sql:2
VAL_EXTRACT.prc-46:sql:2
VAL_EXTRACT.prc-47:sql:2
VAL_EXTRACT.prc-48.1.1:sql:2
VAL_EXTRACT.prc-48:sql:2
VAL_EXTRACT.prc-49:sql:2
VAL_EXTRACT.prc-50:sql:2
VAL_EXTRACT.prc-51.1.1:sql:2
VAL_EXTRACT.prc-51:sql:2
VAL_EXTRACT.prc-52:sql:2
VAL_EXTRACT.prc-53:sql:2
VAL_EXTRACT.prc-54:sql:2
VAL_EXTRACT.prc-55:sql:2
VAL_EXT_ANNUITY.sql-3.1.1:sql:1
VAL_EXT_ANNUITY.sql-7:sql:1
VAL_EXT_ANNUITY.sql-8:sql:1
VAL_EXT_AQUA_BULK_PREM.sql-4:sql:1
VAL_EXT_BNFT_COMP.sql-6:sql:1
VAL_EXT_BNFT_COMP.sql-7:sql:1
VAL_EXT_BNFT_COMP_CHG.sql-4:sql:1
VAL_EXT_CHILD_BNFT.sql-4:sql:1
VAL_EXT_CHILD_BNFT.sql-5:sql:1
VAL_EXT_CTRD_BNFT.sql-5.1.1:sql:1
VAL_EXT_CTRD_BNFT.sql-7:sql:1

I am stuck on how to compare the version numbers and therefore return the latest and greatest.

Any ideas/suggestions would be very welcome !



Posted by admin (Graham Ellis), 3 June 2005
Hiya ... it's Friday afternoon, so please excuse a "Bull at a gate" approach.    Test code:

Code:
@stuff = <DATA>;

@inorder = sort byobanversion @stuff;

foreach $item (@inorder) {
       ($name,$version) = ($item =~ /^([^\d]+)-([\d\.]+)/);
       $object{$name} = $version;
       # print $item;  # for test / debug
}

foreach $obname (sort keys %object) {
       printf ("%-12s %s\n",$object{$obname}, $obname);
       }

sub byobanversion {
       ($a_name,$a_vers) = ($a =~ /^([^\d]+)-([\d\.]+)/);
       ($b_name,$b_vers) = ($b =~ /^([^\d]+)-([\d\.]+)/);
       unless ($a_name cmp $b_name) {
               @a_flds = split(/\./,$a_vers);
               @b_flds = split(/\./,$b_vers);
               for ($k=0; $k<@a_flds; $k++) {
                       if ($k >= @b_flds) { return 1;}
                       $sfd = ($a_flds[$k] <=> $b_flds[$k]);
                       if ($sfd) { return $sfd; }
                       }
               if (@b_flds > @a_flds) { return -1; }
               return 0;
       }
}

__END__
ACT_VAL_MOVEMENTS_SUMMARY.prc-3:sql:1
BOB_BENEFIT_COMPONENT.vw-16:ascii:2
BOB_ESCALATION_BASE.vw-10:ascii:2
BOB_ESCALATION_BASE.vw-11:ascii:2
BOB_GMP_COMPONENT.vw-17:ascii:2
BOB_PR_COMPONENT.vw-12:ascii:2
BasePkgs.sql-4.1.6:sql:2
BasePkgs.sql-4.1.8:sql:2
BasePkgs.sql-4.1.7:sql:2
F_GET_ANNUAL_AMT_PENCE.fnc-3:sql:1
LifeJoin.sql-4:sql:5
LifeJoin.sql-5:sql:5
PLA_error_summary.sh-8:shsrc:1
UAPS.pkg-3.1.27:ascii:1
UAPS_BENEFITCOMPONENT.pkg-11:ascii:1
UAPS_BENEFITCOMPONENT.pkg-12:ascii:1
UAPS_BENEFITCOMPONENT.pkg-10.1.11:ascii:1
UAPS_CONTRACTEDBENEFIT.pkg-13.1.14:ascii:1
UAPS_CONTRACTEDBENEFIT.pkg-13.1.15:ascii:1
UAPS_ESCALATIONBASE.pkg-10:ascii:1
UAPS_ESCALATIONBASE.pkg-12:ascii:1
UAPS_ESCALATIONBASE.pkg-11:ascii:1
UAPS_GMPCOMPONENT.pkg-13:ascii:1
VAL_EXTRACT.prc-43:sql:2
VAL_EXTRACT.prc-44:sql:2
VAL_EXTRACT.prc-45:sql:2
VAL_EXTRACT.prc-46:sql:2
VAL_EXTRACT.prc-47:sql:2
VAL_EXTRACT.prc-48.1.1:sql:2
VAL_EXTRACT.prc-48:sql:2
VAL_EXTRACT.prc-49:sql:2
VAL_EXTRACT.prc-50:sql:2
VAL_EXTRACT.prc-51.1.1:sql:2
VAL_EXTRACT.prc-51:sql:2
VAL_EXTRACT.prc-52:sql:2
VAL_EXTRACT.prc-53:sql:2
VAL_EXTRACT.prc-55:sql:2
VAL_EXTRACT.prc-54:sql:2
VAL_EXT_ANNUITY.sql-3.1.1:sql:1
VAL_EXT_ANNUITY.sql-7:sql:1
VAL_EXT_ANNUITY.sql-8:sql:1
VAL_EXT_AQUA_BULK_PREM.sql-4:sql:1
VAL_EXT_BNFT_COMP.sql-6:sql:1
VAL_EXT_BNFT_COMP.sql-7:sql:1
VAL_EXT_BNFT_COMP_CHG.sql-4:sql:1
VAL_EXT_CHILD_BNFT.sql-4:sql:1
VAL_EXT_CHILD_BNFT.sql-5:sql:1
VAL_EXT_CTRD_BNFT.sql-5.1.1:sql:1
VAL_EXT_CTRD_BNFT.sql-7:sql:1


Results:

Code:
earth-wind-and-fire:~/jun05 grahamellis$ perl hce
3            ACT_VAL_MOVEMENTS_SUMMARY.prc
16           BOB_BENEFIT_COMPONENT.vw
11           BOB_ESCALATION_BASE.vw
17           BOB_GMP_COMPONENT.vw
12           BOB_PR_COMPONENT.vw
4.1.8        BasePkgs.sql
3            F_GET_ANNUAL_AMT_PENCE.fnc
5            LifeJoin.sql
8            PLA_error_summary.sh
3.1.27       UAPS.pkg
12           UAPS_BENEFITCOMPONENT.pkg
13.1.15      UAPS_CONTRACTEDBENEFIT.pkg
12           UAPS_ESCALATIONBASE.pkg
13           UAPS_GMPCOMPONENT.pkg
55           VAL_EXTRACT.prc
8            VAL_EXT_ANNUITY.sql
4            VAL_EXT_AQUA_BULK_PREM.sql
7            VAL_EXT_BNFT_COMP.sql
4            VAL_EXT_BNFT_COMP_CHG.sql
5            VAL_EXT_CHILD_BNFT.sql
7            VAL_EXT_CTRD_BNFT.sql
earth-wind-and-fire:~/jun05 grahamellis$


Looks OK - but TEST IT FURTHER - your data didn't contain too many test cases our of order there may still be some bugs in my code.

Method - sort all the incoming records, then set up a has for each named object so that the later ones override the earlier ones.  Once the hash is fully set up, it includes only the latest version numbers.

The sort algorithm splits out product name and release numbers and compares product name first.  Where the product name is the same it loops through each dot separated parts of the version number and returns either +1 or -1 when it finds a difference. If it runs out of parts from $a first, it returns -1 and if it runs out of parts from $b first it returns +1.   If both run out at the same time, it returns 0.

Posted by admin (Graham Ellis), 3 June 2005
P.S.  I moved some of your data lines around to test the sorting a little more thoroughly ....

Posted by smithce (smithce), 6 June 2005


Well...what can I say.

I need to do some further testing but that is amazing ...does exactly what is required.

Thanks you very much. I saw this Saturday morning but could not test it until today and it has certainly cheered up my dreary Monday !!!

Many thanks
Clive




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho