My company has multiple product lines and the version numbers are in different format.
For instance, one product line's version numbers may look like this: 5, 5.2, 5.11. The highest version in this example is 5.11. Therefore, we cannot simply sort them numerically.
Another product line's version numbers may look like this: 6.1.11, 6.1.22, 6.2.3, 6.2.11. This would make sorting more difficult.
And right now, the longest version number looks like this: i.j.k.l.
I want to write a piece of perl code to sort these version numbers out and it should be designed to handle future version numbers in a generic format: i.j.k. ... .m. ... .n.
I came up a solution which can handle the current situations. But it's semi-hard coded. I am hoping someone here could make it more robust.
My codes are listed below:
First sample run:
And the 2nd sample run:
In summary, the results are ok. Just the implementation is not robust at all.
Thank you for your time and help.
For instance, one product line's version numbers may look like this: 5, 5.2, 5.11. The highest version in this example is 5.11. Therefore, we cannot simply sort them numerically.
Another product line's version numbers may look like this: 6.1.11, 6.1.22, 6.2.3, 6.2.11. This would make sorting more difficult.
And right now, the longest version number looks like this: i.j.k.l.
I want to write a piece of perl code to sort these version numbers out and it should be designed to handle future version numbers in a generic format: i.j.k. ... .m. ... .n.
I came up a solution which can handle the current situations. But it's semi-hard coded. I am hoping someone here could make it more robust.
My codes are listed below:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $tail = 't';
my @ver = (6.1, '6.1.2', '6.1.11'); [COLOR=#3465A4][b]# this line is for 1st run[/b][/color]
#@ver = (5.2, 5, 5.3, 5.12); [COLOR=#3465A4][b]# enable this line in 2nd run[/b][/color]
my $info = &a2h(\@ver);
print Dumper($info);
&pickHighest($info);
[COLOR=#F57900][b]# Method pickHighest() is new and is somewhat hard coded.
# 1) I am not sure if the implementation is on the right track?
# 2) If it's on the right track, how to make it more robust?[/b][/color]
sub pickHighest {
my $verInfo = $_[0];
my @v1 = sort numeric (keys(%{$verInfo}));
print "Highest Major Release: $v1[$#v1]\n";
if(ref($verInfo->{$v1[$#v1]}) eq 'HASH') {
my @v2 = sort numeric (keys(%{$verInfo->{$v1[$#v1]}}));
print "Highest Sub-Major Release: $v2[$#v2]\n";
if(ref($verInfo->{$v1[$#v1]}->{$v2[$#v2]}) eq 'HASH') {
my @v3 = sort numeric (keys(%{$verInfo->{$v1[$#v1]}->{$v2[$#v2]}}));
print "Highest Minor Release: $v3[$#v3]\n";
if(ref($verInfo->{$v1[$#v1]}->{$v2[$#v2]}->{$v3[$#v3]}) eq 'HASH') {
[COLOR=#EF2929][b]# To be implemented...
# If there was a version number like this: i.j.k. ... .m.n
# then this kind of if/else block would go ridiculously long
# and I'd call it semi-hardcode, which I hate it.[/b][/color]
}
else {
print "This is the highest release: $v1[$#v1]\.$v2[$#v2]\.$v3[$#v3]\n";
}
}
else {
print "This is the highest release: $v1[$#v1]\.$v2[$#v2]\n";
}
}
else {
print "This is the highest release: $v1[$#v1]\n";
}
}
exit;
[COLOR=#F57900][b]# Method a2h() was discussed yesterday in this [url=http://www.tek-tips.com/viewthread.cfm?qid=1707676]thread[/url]
# This method can handle a version number like this: i.j.k. ... .m.n[/b][/color]
sub a2h {
my $a = $_[0];
my %h = ();
foreach my $x (@{$a}) {
my @field = split(/\./, $x);
my $hRef = \%h;
my $hPrev;
my $l = scalar @field;
for (my $i = 0; $i < $l; $i++) {
$hPrev->{$field[$i - 1]} = $hRef = {} unless ref $hRef eq 'HASH';
$hRef->{$field[$i]} = $i < $l - 1 ? {} : $tail unless exists $hRef->{$field[$i]};
$hPrev = $hRef;
$hRef = $hRef->{$field[$i]};
}
}
return \%h;
}
sub numeric { $a <=> $b; }
First sample run:
Code:
% ./sortVer.pl
$VAR1 = {
'6' => {
'1' => {
'11' => 't',
'2' => 't'
}
}
};
Highest Major Release: 6
Highest Sub-Major Release: 1
Highest Minor Release: 11
This is the highest release: 6.1.11
And the 2nd sample run:
Code:
% ./sortVer.pl
$VAR1 = {
'5' => {
'3' => 't',
'12' => 't',
'2' => 't'
}
};
Highest Major Release: 5
Highest Sub-Major Release: 12
This is the highest release: 5.12
In summary, the results are ok. Just the implementation is not robust at all.
Thank you for your time and help.