leons has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; sub byVersion; my ($v1,$v2)=("1.1.1.1","1.1.1.2"); print "$v1 is equal to $v2\n" if (byVersion($v1,$v2)== 0); print "$v1 is greater than $v2\n" if (byVersion($v1,$v2)== 1); print "$v1 is less than $v2\n" if (byVersion($v1,$v2)==-1); sub byVersion { my @First=split /\./,$a||shift; my @Second=split /\./,$b||shift; foreach(0..(($#First>$#Second)?$#Second:$#First)) { next if ($First[$_] eq $Second[$_]); return (($First[$_]=~/^\d+$/o)&&($Second[$_]=~/^\d+$/o))?($First[$_] +<=>$Second[$_]):($First[$_] cmp $Second[$_]); } return ($#First<=>$#Second); }
#!/usr/bin/perl -w use strict; sub byVersion; my ($v1,$v2)=("1.1.1.1","1.1.1.2"); #----------------------------- my @row=(1,2,3,4); my @nrow=sort byVersion @row; print "@nrow\n"; #----------------------------- print "$v1 is equal to $v2\n" if (byVersion($v1,$v2)== 0); print "$v1 is greater than $v2\n" if (byVersion($v1,$v2)== 1); print "$v1 is less than $v2\n" if (byVersion($v1,$v2)==-1); sub byVersion { my @First=split /\./,$a||shift; my @Second=split /\./,$b||shift; foreach(0..(($#First>$#Second)?$#Second:$#First)) { next if ($First[$_] eq $Second[$_]); return (($First[$_]=~/^\d+$/o)&&($Second[$_]=~/^\d+$/o))?($First[$_] +<=>$Second[$_]):($First[$_] cmp $Second[$_]); } return ($#First<=>$#Second); }
my ($val1,$val2)=("1.1.2","1.1.3"); print "Greater than\n" if ((sort byVersion ($val1,$val2))[-1] eq $val1 +);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort and related stuff
by Corion (Patriarch) on Feb 23, 2001 at 15:44 UTC | |
by leons (Pilgrim) on Feb 23, 2001 at 15:58 UTC | |
by Corion (Patriarch) on Feb 23, 2001 at 16:39 UTC | |
by tilly (Archbishop) on Feb 24, 2001 at 07:17 UTC | |
|
Re: Sort and related stuff
by Tyke (Pilgrim) on Feb 23, 2001 at 15:29 UTC | |
|
Re: Sort and related stuff
by davorg (Chancellor) on Feb 23, 2001 at 15:25 UTC | |
by leons (Pilgrim) on Feb 23, 2001 at 15:33 UTC | |
|
Re: Sort and related stuff
by leons (Pilgrim) on Feb 23, 2001 at 15:25 UTC | |
|
(ichimunki) re: Sort and related stuff
by ichimunki (Priest) on Feb 23, 2001 at 17:38 UTC | |
|
Re: Sort and related stuff
by sierrathedog04 (Hermit) on Feb 23, 2001 at 17:37 UTC | |
by ichimunki (Priest) on Feb 23, 2001 at 17:47 UTC | |
|
Re: Sort and related stuff
by goldclaw (Scribe) on Feb 23, 2001 at 18:23 UTC | |
|
Re: Sort and related stuff
by turnstep (Parson) on Feb 24, 2001 at 06:00 UTC | |
|
Re: Sort and related stuff
by princepawn (Parson) on Feb 24, 2001 at 01:26 UTC |