[29924] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1167 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 3 06:55:09 2008

Date: Thu, 3 Jan 2008 03:54:57 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 3 Jan 2008     Volume: 11 Number: 1167

Today's topics:
        Strip equal portion of strings / version comparison <s1037989@gmail.com>
    Re: Strip equal portion of strings / version comparison (Greg Bacon)
    Re: Strip equal portion of strings / version comparison <s1037989@gmail.com>
    Re: Strip equal portion of strings / version comparison (Greg Bacon)
    Re: Strip equal portion of strings / version comparison <ced@blv-sam-01.ca.boeing.com>
        utf-8 julia_2683@hotmail.com
    Re: utf-8 <joost@zeekat.nl>
        Why does undef->{id} give a warning and $undefined_var- himanshu.garg@gmail.com
        Why doesn't it evaluate in RE using "e" modifier ?? <ahmad.abdulghany@gmail.com>
    Re: Why doesn't it evaluate in RE using "e" modifier ?? <thepoet_nospam@arcor.de>
    Re: Why doesn't it evaluate in RE using "e" modifier ?? <rvtol+news@isolution.nl>
    Re: Why doesn't it evaluate in RE using "e" modifier ?? <mritty@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 1 Jan 2008 10:02:47 -0800 (PST)
From: Stefan <s1037989@gmail.com>
Subject: Strip equal portion of strings / version comparison
Message-Id: <a6fc09a8-bc18-4235-baf5-23e8d9aca8a5@u10g2000prn.googlegroups.com>

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


------------------------------

Date: Tue, 01 Jan 2008 19:17:43 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Strip equal portion of strings / version comparison
Message-Id: <13nl4enr7aet899@corp.supernews.com>

In article <a6fc09a8-bc18-4235-baf5-23e8d9aca8a5@u10g2000prn.googlegroups.com>,
    Stefan  <s1037989@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) = @_;

      return if $a eq $b;

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

    sub cmpv {
      my @a = split /\./, $a;
      my @b = split /\./, $b;

      while (@a && @b) {
        my $a = shift @a;
        my $b = shift @b;

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

      @a <=> @b;
    }

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

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

      my @eq = strip_equal $a, $b;
      my $cmpv = cmpv $a, $b;
      print "a=$a, b=$b:\n",
            "  - strip_equal: [@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?"


------------------------------

Date: Tue, 1 Jan 2008 11:28:14 -0800 (PST)
From: Stefan <s1037989@gmail.com>
Subject: Re: Strip equal portion of strings / version comparison
Message-Id: <f3630f96-57ca-4ff2-b771-59a335948b3d@j20g2000hsi.googlegroups.com>

On Jan 1, 1:17=A0pm, gba...@hiwaay.net (Greg Bacon) wrote:
> In article <a6fc09a8-bc18-4235-baf5-23e8d9aca...@u10g2000prn.googlegroups.=
com>,
> =A0 =A0 Stefan =A0<s1037...@gmail.com> wrote:
>
: snip :
> =A0 =A0 sub strip_equal {
> =A0 =A0 =A0 my($a,$b) =3D @_;
>
> =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 @a =3D split /\./, $a;
> =A0 =A0 =A0 my @b =3D split /\./, $b;
>
> =A0 =A0 =A0 while (@a && @b) {
> =A0 =A0 =A0 =A0 my $a =3D shift @a;
> =A0 =A0 =A0 =A0 my $b =3D shift @b;
>
> =A0 =A0 =A0 =A0 $a <=3D> $b && return $a <=3D> $b;
> =A0 =A0 =A0 }
>
> =A0 =A0 =A0 @a <=3D> @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?"



------------------------------

Date: Tue, 01 Jan 2008 20:15:10 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Strip equal portion of strings / version comparison
Message-Id: <13nl7qetpvifc3@corp.supernews.com>

In article <f3630f96-57ca-4ff2-b771-59a335948b3d@j20g2000hsi.googlegroups.com>,
    Stefan  <s1037989@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


------------------------------

Date: Tue, 1 Jan 2008 21:07:45 -0800 (PST)
From: "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: Strip equal portion of strings / version comparison
Message-Id: <5ed8a099-8477-492a-bcc4-0f82a85cbfab@i7g2000prf.googlegroups.com>

On Jan 1, 10:02 am, Stefan <s1037...@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 @s1 = split /\./, $a;
my $strip1 = $s1[-1];
my @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


------------------------------

Date: Mon, 31 Dec 2007 11:33:00 -0800 (PST)
From: julia_2683@hotmail.com
Subject: utf-8
Message-Id: <1c87f921-f6e2-4a2e-a4eb-5635072b8c65@e23g2000prf.googlegroups.com>

I run perl v5.8.7 and my regular expresion is ($txt =3D~ m/(\w+|=E9\w+)/g)
which do not take every utf-8 word. How to make this regular
expression to take every utf-8 word ?


------------------------------

Date: Mon, 31 Dec 2007 20:45:57 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: utf-8
Message-Id: <87bq86y7ey.fsf@zeekat.nl>

julia_2683@hotmail.com writes:

> I run perl v5.8.7 and my regular expresion is ($txt =~ m/(\w+|é\w+)/g)
> which do not take every utf-8 word. How to make this regular
> expression to take every utf-8 word ?

Just \w should work, provided you're handling your encodings correctly *and*
your $txt is actually utf-8 encoded. This is IMO a bug.

Note that if your script itself is utf8 encoded you need to "use utf8"
somewhere at the top of your script.

For instance:

#/usr/bin/perl -w
use strict;

# set output stream as utf-8 encoded (i have a utf-8 enabled terminal)
binmode STDOUT,":utf8";  

my $str="\x{e9}";    # "é", not necessarily as utf-8 - very likely latin-1
utf8::upgrade($str); # force utf-8 encoding

print "$str was ",($str =~ /\w+/ ? "" : "not "),"matched\n";

Joost.


------------------------------

Date: Thu, 3 Jan 2008 01:00:16 -0800 (PST)
From: himanshu.garg@gmail.com
Subject: Why does undef->{id} give a warning and $undefined_var->{id} doesn't
Message-Id: <fa49f7b1-e543-4e22-bce5-d4199d50154d@e6g2000prf.googlegroups.com>

% cat test.pl
use Test::Simple tests => 2;
use strict;
my $undefined_var;
ok(!$undefined_var->{id}, '!$undefined_var->{id}');
ok(!undef->{id}, '!undef->{id}')

% perl test.pl
1..2
ok 1 - !$request->{id}
Can't use an undefined value as a HASH reference at test.pl line 5.
# Looks like you planned 2 tests but only ran 1.
# Looks like your test died just after 1.


------------------------------

Date: Wed, 2 Jan 2008 02:50:30 -0800 (PST)
From: Ahmad <ahmad.abdulghany@gmail.com>
Subject: Why doesn't it evaluate in RE using "e" modifier ??
Message-Id: <40223c64-d934-4961-bcf3-a3c948032f4f@e6g2000prf.googlegroups.com>

Using the following example:

$ab=7;
$str='$ab';
$str=~s/(\$\w+)/3+$1/eeeg ;
print $str;

It returns 3 (I expect 10!!!) Where's the mistake?
thanks and regards,
Ahmad


------------------------------

Date: Wed, 02 Jan 2008 13:10:36 +0100
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: Why doesn't it evaluate in RE using "e" modifier ??
Message-Id: <477b7f3d$0$17535$9b4e6d93@newsspool4.arcor-online.net>

Ahmad wrote:
> Using the following example:
> 
> $ab=7;
> $str='$ab';
> $str=~s/(\$\w+)/3+$1/eeeg ;
> print $str;
> 
> It returns 3 (I expect 10!!!) Where's the mistake?

The first evaluation of the replacement pattern already tries
to execute the addition, but at this point the second operator
of the addition is still the string '$ab', which evaluates to
zero in numeric context. To have it behave correctly, you need
to make sure that the expression returns the final term only in
the last step (btw., three "e" modifiers are overkill here):

$str=~s/(\$\w+)/"3 + " . $1/eeg;

HTH
-Chris


------------------------------

Date: Wed, 2 Jan 2008 13:10:51 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Why doesn't it evaluate in RE using "e" modifier ??
Message-Id: <flg2hr.1jg.1@news.isolution.nl>

Ahmad schreef:
> Using the following example:
> 
> $ab=7;
> $str='$ab';
> $str=~s/(\$\w+)/3+$1/eeeg ;
> print $str;
> 
> It returns 3 (I expect 10!!!) Where's the mistake?

See http://template-toolkit.org/ 

-- 
Affijn, Ruud

"Gewoon is een tijger."


------------------------------

Date: Wed, 2 Jan 2008 07:30:33 -0800 (PST)
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Why doesn't it evaluate in RE using "e" modifier ??
Message-Id: <ad28627c-6756-4f5e-80a5-f97fc258fd82@u10g2000prn.googlegroups.com>

On Jan 2, 5:50=A0am, Ahmad <ahmad.abdulgh...@gmail.com> wrote:
> Using the following example:
>
> $ab=3D7;
> $str=3D'$ab';
> ;
> print $str;
>
> It returns 3 (I expect 10!!!) Where's the mistake?

Each /e is equivalent to an eval().  So you have:

eval(eval(eval('3 + $1')));
doing the first eval, that comes out to:
eval(eval(3 + '$ab'))
3 + '$ab' of course, is 3, since '$ab' is a string being used in
numeric context
so now you have
eval(eval(3))
which of course is
eval(3)
which of course is
3

Instead, you need the string you're trying to evaluate to be built
from the actual value of $ab:

eval(eval('3 + ' . $1))
eval('3 + $ab')
3 + 7
10

So change your s/// to:
$str=3D~s/(\$\w+)/'3 + ' . $1/eeg

Paul Lalli


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1167
***************************************


home help back first fref pref prev next nref lref last post