Skip to content

Instantly share code, notes, and snippets.

@chizmw
Created January 30, 2013 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chizmw/4678385 to your computer and use it in GitHub Desktop.
Save chizmw/4678385 to your computer and use it in GitHub Desktop.
Comparing some version numbers for a post on http://blogs.perl.org/users/chisel
#!/usr/bin/env perl
use strict;
use warnings;
use feature ':5.12';
use version;
use Text::SimpleTable;
my @versionstrings = (
'0.0.4',
'0.0.5',
'0.0.5_01',
'0.0.5_50',
'0.0.5_99',
'0.0.5',
'0.0.6',
);
my @versions;
foreach my $vstring (@versionstrings) {
push @versions, version->parse($vstring);
}
my $t1 = Text::SimpleTable->new(
[12, 'Input String'],
[12, 'normal()'],
[14, 'cmp-prev'],
[14, 'cmp-next'],
);
for (my$i=0; $i<@versions; $i++) {
my $cmp_with_prev;
if($i>0) {
given ($i) {
when ($versions[$i] < $versions[$i-1]) {
$cmp_with_prev = "< " . $versions[$i-1];
}
when ($versions[$i] == $versions[$i-1]) {
$cmp_with_prev = "== " . $versions[$i-1];
}
when ($versions[$i] > $versions[$i-1]) {
$cmp_with_prev = " > " . $versions[$i-1];
}
}
}
else {
$cmp_with_prev = '';
}
my $cmp_with_next;
if($i<@versions-1) {
given ($i) {
when ($versions[$i] < $versions[$i+1]) {
$cmp_with_next = "< " . $versions[$i+1];
}
when ($versions[$i] == $versions[$i+1]) {
$cmp_with_next = "== " . $versions[$i+1];
}
when ($versions[$i] > $versions[$i+1]) {
$cmp_with_next = " > " . $versions[$i+1];
}
}
}
else {
$cmp_with_next = '';
}
$t1->row(
$versionstrings[$i],
$versions[$i]->normal,
$cmp_with_prev,
$cmp_with_next,
);
}
say $t1->draw;
@wols
Copy link

wols commented Feb 26, 2016

Thanks for post here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment