Strip equal portion of strings / version comparison

Starting with:

$a = '1.1.2';
$b = '1.1.10';

Is there an elegant, one-liner way -- possibly using RE -- to yield:

$a = '2';
$b = '10';

In other words, strip out the beginning equal portion of 2 strings?

Moreover, how could you compare the original $a and $b such that $b >
$a (as in version comparisons)?

Thanks!
Stefan
s1037989 [ Di, 01 Januar 2008 19:02 ] [ ID #1897492 ]

Re: Strip equal portion of strings / version comparison

In article <a6fc09a8-bc18-4235-baf5-23e8d9aca8a5 [at] u10g2000prn.googlegroups.com>,
Stefan <s1037989 [at] gmail.com> wrote:

: Starting with:
:
: $a = '1.1.2';
: $b = '1.1.10';
:
: Is there an elegant, one-liner way -- possibly using RE -- to yield:
:
: $a = '2';
: $b = '10';
:
: In other words, strip out the beginning equal portion of 2 strings?
:
: Moreover, how could you compare the original $a and $b such that $b >
: $a (as in version comparisons)?

Consider the following code:

$ cat try
#! /usr/bin/perl

use warnings;
use strict;

sub strip_equal {
my($a,$b) = [at] _;

return if $a eq $b;

local $_ = "$a;$b";
return ($2,$3) if /^(.+\.)(.+);\1(.+)$/;
}

sub cmpv {
my [at] a = split /\./, $a;
my [at] b = split /\./, $b;

while ( [at] a && [at] b) {
my $a = shift [at] a;
my $b = shift [at] b;

$a <=> $b && return $a <=> $b;
}

[at] a <=> [at] b;
}

my [at] cases = (
["1.1.10", "1.1.2"],
["1.1.2", "1.1.2"],
["1.2.3", "4.5.6"],
);

$" = "][";
foreach my $case ( [at] cases) {
our($a,$b) = [at] $case;

my [at] eq = strip_equal $a, $b;
my $cmpv = cmpv $a, $b;
print "a=$a, b=$b:\n",
" - strip_equal: [ [at] eq]\n",
" - cmpv: $cmpv\n";
}
$ ./try
a=1.1.10, b=1.1.2:
- strip_equal: [10][2]
- cmpv: 1
a=1.1.2, b=1.1.2:
- strip_equal: []
- cmpv: 0
a=1.2.3, b=4.5.6:
- strip_equal: []
- cmpv: -1

Hope this helps,
Greg
--
For diagrams comprehensiveness is the enemy of comprehensibility.
-- Martin Fowler, "Is Design Dead?"
gbacon [ Di, 01 Januar 2008 20:17 ] [ ID #1897493 ]

Re: Strip equal portion of strings / version comparison

On Jan 1, 1:17=A0pm, gba... [at] hiwaay.net (Greg Bacon) wrote:
> In article <a6fc09a8-bc18-4235-baf5-23e8d9aca... [at] u10g2000prn.googlegroups.=
com>,
> =A0 =A0 Stefan =A0<s1037... [at] gmail.com> wrote:
>
: snip :
> =A0 =A0 sub strip_equal {
> =A0 =A0 =A0 my($a,$b) =3D [at] _;
>
> =A0 =A0 =A0 return if $a eq $b;
>
> =A0 =A0 =A0 local $_ =3D "$a;$b";
> =A0 =A0 =A0 return ($2,$3) if /^(.+\.)(.+);\1(.+)$/;
> =A0 =A0 }
> =A0 =A0 sub cmpv {
> =A0 =A0 =A0 my [at] a =3D split /\./, $a;
> =A0 =A0 =A0 my [at] b =3D split /\./, $b;
>
> =A0 =A0 =A0 while ( [at] a && [at] b) {
> =A0 =A0 =A0 =A0 my $a =3D shift [at] a;
> =A0 =A0 =A0 =A0 my $b =3D shift [at] b;
>
> =A0 =A0 =A0 =A0 $a <=3D> $b && return $a <=3D> $b;
> =A0 =A0 =A0 }
>
> =A0 =A0 =A0 [at] a <=3D> [at] b;
> =A0 =A0 }
: snip :

Those are some great functions!! Thanks!!

>
> Hope this helps,
> Greg
> --
> For diagrams comprehensiveness is the enemy of comprehensibility.
> =A0 =A0 -- Martin Fowler, "Is Design Dead?"
s1037989 [ Di, 01 Januar 2008 20:28 ] [ ID #1897494 ]

Re: Strip equal portion of strings / version comparison

In article <f3630f96-57ca-4ff2-b771-59a335948b3d [at] j20g2000hsi.googlegroups.com>,
Stefan <s1037989 [at] gmail.com> wrote:

: Those are some great functions!! Thanks!!

I'm glad you liked them. Happy new year!

Greg
--
He who proclaims the godliness of the State and the infallibility of
its priests, the bureaucrats, is considered as an impartial student of
the social sciences. All those raising objections are branded as biased
and narrow-minded. -- Ludwig von Mises, Planned Chaos
gbacon [ Di, 01 Januar 2008 21:15 ] [ ID #1897498 ]

Re: Strip equal portion of strings / version comparison

On Jan 1, 10:02 am, Stefan <s1037... [at] gmail.com> wrote:
> Starting with:
>
> $a = '1.1.2';
> $b = '1.1.10';
>
> Is there an elegant, one-liner way -- possibly using RE -- to yield:
>
> $a = '2';
> $b = '10';
>
> In other words, strip out the beginning equal portion of 2 strings?
>
> Moreover, how could you compare the original $a and $b such that $b >
> $a (as in version comparisons)?
>

Yet another way:

#! perl -l
use strict; use warnings;
use List::Util qw/max/;

my [at] s1 = split /\./, $a;
my $strip1 = $s1[-1];
my [at] s2 = split /\./, $b;
my $strip2 = $s2[-1];

CMP: {
for ( 0 .. max( $#s1, $#s2 ) ) {
my $res = ($s1[$_] || 0) <=> ($s2[$_] || 0);
$res == -1 and print "a is less than b"
and last CMP;
$res == 1 and print "a is greater than b"
and last CMP;
}
print "a is equal to b";
}
__END__


--
Charles DeRykus
Charles DeRykus [ Mi, 02 Januar 2008 06:07 ] [ ID #1898349 ]
Perl » comp.lang.perl.misc » Strip equal portion of strings / version comparison

Vorheriges Thema: FAQ 6.18 Why does using $&, $`, or $' slow my program down?
Nächstes Thema: FAQ 6.13 How do I process each word on each line?