in reply to Re^2: Advanced Bubble Sort
in thread Advanced Bubble Sort
In this case we have a very small data example to work from. I did put in a print so the OP can see the default sort order and determine whether or not this applies more generally to his/her problem or not. Version numbers often, but not always have leading zero's or a fixed number of digits.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Advanced Bubble Sort
by AnomalousMonk (Archbishop) on Oct 14, 2016 at 17:41 UTC | |
In this case we have a very small data example to work from. A frustratingly small data sample, and if this were $work, I (and I suspect you, too) would instantly be busy calling on the phone or composing an e-mail to the "client" to answer the question "Just what the heck do you really want here?" ... in the OP's case, the default sort order does work. The legal and ethical obligations of work don't apply in this place, but I still think there is, so to speak, a "moral" obligation (appropriate to a monastery!) to offer to a supplicant defensive measures suitable to the question. After all, none of the example version numbers have leading zeros, and how did, e.g., 'abc-4.2.11-8.el7_2.x86_64' get where it is without first going through 'abc-4.2.9-8.el7_2.x86_64', and might it not yet go to 'abc-4.2.11-10.el7_2.x86_64'? ("I didn't get where I am today without going through 'abc-4.2.9-8.el7_2.x86_64'!" Old Reginald Perrin reference. Never mind...) Give a man a fish: <%-{-{-{-< | [reply] [d/l] |
by Marshall (Canon) on Oct 17, 2016 at 05:48 UTC | |
If it can be determined exactly what the "version" string is, then I perhaps would use an algorithm like below. One pass through the data, no sorting. Use a hash table with a key of the "generic name" and value of the version. If the version is greater, then replace the hash value with the greater version. Of course this can become very complex in the real world because version naming conventions vary widely. However, I think this was a successful thread in that the main issues have been discussed and some techniques demo'd. I did this quickly and perhaps there are some minor point to quibble about. But I think the general idea of this approach is solid.
| [reply] [d/l] |
by AnomalousMonk (Archbishop) on Oct 17, 2016 at 07:31 UTC | |
FWIW, here's my latest take on the problem. I also have some doubt about what a "version string" is in this context, but like you, I take, e.g., "4.2.12-7" to be a version string with four components. I also wanted to investigate the single pass, no sort approach. The following solution also retains input order a-la List::MoreUtils::uniq(). (I have no idea if these latter features are required or desired at all, but I wanted to play around with them just as an intellectual exercise.) I can't say I really like this code; three of the map (This runs under 5.8.9.) Output:
Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |