[31528] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2787 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 25 00:09:29 2010

Date: Sun, 24 Jan 2010 21:09:12 -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           Sun, 24 Jan 2010     Volume: 11 Number: 2787

Today's topics:
        How to find every occurrence of a substring by a functi <pengyu.ut@gmail.com>
    Re: How to find every occurrence of a substring by a fu <someone@example.com>
    Re: How to find every occurrence of a substring by a fu <jurgenex@hotmail.com>
    Re: How to find every occurrence of a substring by a fu <pengyu.ut@gmail.com>
    Re: How to find every occurrence of a substring by a fu <someone@example.com>
        LibXML (XPATH) and escape <fgnowfg@gmail.com>
    Re: macros: return or exit <marc.girod@gmail.com>
    Re: macros: return or exit <uri@StemSystems.com>
    Re: open pragma appears to have no effect <sreservoir@gmail.com>
    Re: open pragma appears to have no effect <burner+usenet@imf.au.dk>
    Re: open pragma appears to have no effect <sreservoir@gmail.com>
        substitution without affecting the input string <pengyu.ut@gmail.com>
    Re: substitution without affecting the input string <sreservoir@gmail.com>
    Re: substitution without affecting the input string <pengyu.ut@gmail.com>
    Re: substitution without affecting the input string <tadmc@seesig.invalid>
    Re: substitution without affecting the input string <sreservoir@gmail.com>
    Re: User-defined substitution <m@rtij.nl.invlalid>
    Re: User-defined substitution sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 24 Jan 2010 18:36:11 -0800 (PST)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: How to find every occurrence of a substring by a function?
Message-Id: <2ed24990-a676-4a79-b1a2-0bbf789f0226@v25g2000yqk.googlegroups.com>

Example 3b on the following page use a loop to find all substring.

http://perlmeme.org/howtos/perlfunc/index_function.html

I'm wondering if there is a function in perl library that can give me
back all the matches, just like findall in python (see section 3.3 in
the following page).

http://www.amk.ca/python/howto/regex/


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

Date: Sun, 24 Jan 2010 18:49:05 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: How to find every occurrence of a substring by a function?
Message-Id: <DE77n.12192$BV.4704@newsfe07.iad>

Peng Yu wrote:
> Example 3b on the following page use a loop to find all substring.
> 
> http://perlmeme.org/howtos/perlfunc/index_function.html
> 
> I'm wondering if there is a function in perl library that can give me
> back all the matches, just like findall in python (see section 3.3 in
> the following page).
> 
> http://www.amk.ca/python/howto/regex/

Sure:

my @findall = $string =~ /pattern/g;




John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway


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

Date: Sun, 24 Jan 2010 18:54:28 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to find every occurrence of a substring by a function?
Message-Id: <hn1ql51age7ptr3lo9aa93ccr8i3ca0p70@4ax.com>

Peng Yu <pengyu.ut@gmail.com> wrote:
>Example 3b on the following page use a loop to find all substring.
>
>http://perlmeme.org/howtos/perlfunc/index_function.html
>
>I'm wondering if there is a function in perl library that can give me
>back all the matches,

Maybe I misunderstand what you are looking for but it appears to me that
the g-modifier in list context does exactly what you want. From 'perldoc
perlretut':

    	In list context, "//g" returns a list of matched groupings, or 
	if there are no groupings, a list of matches to the whole 
	regexp. 

jue


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

Date: Sun, 24 Jan 2010 19:05:15 -0800 (PST)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to find every occurrence of a substring by a function?
Message-Id: <f3f8490e-6c65-44da-be46-23542cf657a7@g1g2000yqi.googlegroups.com>

On Jan 24, 8:49=A0pm, "John W. Krahn" <some...@example.com> wrote:
> Peng Yu wrote:
> > Example 3b on the following page use a loop to find all substring.
>
> >http://perlmeme.org/howtos/perlfunc/index_function.html
>
> > I'm wondering if there is a function in perl library that can give me
> > back all the matches, just like findall in python (see section 3.3 in
> > the following page).
>
> >http://www.amk.ca/python/howto/regex/
>
> Sure:
>
> my @findall =3D $string =3D~ /pattern/g;

I think that I didn't make it clear. I want the location of the match.

For example, for the following variables, I want get an array that has
the number 0 and 7. You command gives an array of two same strings
'abc' and 'abc'.

$string=3D"abcdefgabc";
$substring=3D"abc";




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

Date: Sun, 24 Jan 2010 21:07:58 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: How to find every occurrence of a substring by a function?
Message-Id: <PG97n.18923$U83.11493@newsfe10.iad>

Peng Yu wrote:
> On Jan 24, 8:49 pm, "John W. Krahn" <some...@example.com> wrote:
>> Peng Yu wrote:
>>> Example 3b on the following page use a loop to find all substring.
>>> http://perlmeme.org/howtos/perlfunc/index_function.html
>>> I'm wondering if there is a function in perl library that can give me
>>> back all the matches, just like findall in python (see section 3.3 in
>>> the following page).
>>> http://www.amk.ca/python/howto/regex/
>> Sure:
>>
>> my @findall = $string =~ /pattern/g;
> 
> I think that I didn't make it clear. I want the location of the match.

You said that you wanted something "just like findall" which according 
to the page you referenced "Find all substrings where the RE matches, 
and returns them as a list." which is exactly what the Perl code above does.


> For example, for the following variables, I want get an array that has
> the number 0 and 7. You command gives an array of two same strings
> 'abc' and 'abc'.
> 
> $string="abcdefgabc";
> $substring="abc";

$ perl -le'
my $string    = "abcdefgabc";
my $substring = "abc";
my @offsets;
{   use re "eval";
     $string =~ /\Q$substring\E(??{ push @offsets, $-[ 0 ] })/g;
     }
print "@offsets";
'
0 7




John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway


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

Date: Sun, 24 Jan 2010 09:23:51 -0800 (PST)
From: Faith Greenwood <fgnowfg@gmail.com>
Subject: LibXML (XPATH) and escape
Message-Id: <2f662174-458f-4554-b927-0d3e85f77d91@x9g2000vbo.googlegroups.com>

I am trying to escape single and double quotation marks in an XPATH
expression. Failing to escape the quotes produces an "Invalid
predicate" error.

Here's an example:

my $value="Dave's";
$value =~s/\'/\\\'/g;
my $search="//entry/owner/person[value/text()='$value']/../../this/
animal/value/text()";

However, this also produces the "Invalid predicate" error. I've tried
what the xpath language suggests by replacing a ' with &apos;

my $value="Dave's";
$value =~s/\'/&apos;/g;
my $search="//entry/owner/person[value/text()='$value']/../../this/
animal/value/text()";

This time, though, the search produces no results. (I know the
expression itself is valid as it works on $values that contain no
single or double quotes).

What is the correct way to escape quotation characters for XPath
expressions in perl?



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

Date: Sun, 24 Jan 2010 12:03:34 -0800 (PST)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: macros: return or exit
Message-Id: <9d7e214a-d894-46d9-9f4b-ae07dfc27ab5@y12g2000yqh.googlegroups.com>

On Jan 21, 1:14=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:

> =A0 =A0 require Want;
> =A0 =A0 *Nasty::Module::exit =3D sub {
> =A0 =A0 =A0 =A0 my ($rv) =3D @_;
> =A0 =A0 =A0 =A0 Want::double_return();
> =A0 =A0 =A0 =A0 return $rv, "done";
> =A0 =A0 };
>
> This would need to be done before loading Nasty::Module, so the override
> gets properly picked up.

OK. I installed Want, and it works on a demo example but so far not in
my test case.
I had to compile without -fstack-protector, which probably requires a
newer compiler than the one I have on cygwin. It may also be that this
would not be supported on cygwin or whatever.
Anyway, as I told, my demo seems to work, and all the tests passed.

The main problem is however that I can see now that double_return is
too unflexible: exit, exec and die may be invoked at any depth level
in the call stack, not necessarily at level 2.
What I would need with this strategy is a longjump...
I may still try to write it myself...

Or to investigate the other option: based on fork...

Marc


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

Date: Sun, 24 Jan 2010 15:35:26 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: macros: return or exit
Message-Id: <87sk9vz1f5.fsf@quad.sysarch.com>

>>>>> "MG" == Marc Girod <marc.girod@gmail.com> writes:

  MG> The main problem is however that I can see now that double_return is
  MG> too unflexible: exit, exec and die may be invoked at any depth level
  MG> in the call stack, not necessarily at level 2.
  MG> What I would need with this strategy is a longjump...
  MG> I may still try to write it myself...

again, i am baffled as to your big picture. one day ...

but longjump in perl is spelled die/eval. if you wrap your stack of calls
with eval blocks, you can pop up the stack with dies until you hit the
one that handles that particular die. you can tell by passing in a die
string and checking it at each eval and propogating it up if it isn't
handled there.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Sun, 24 Jan 2010 14:30:47 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: open pragma appears to have no effect
Message-Id: <hji75h$m9k$1@speranza.aioe.org>

On 1/24/2010 9:02 AM, Rasmus Villemoes wrote:
> Rasmus Villemoes<burner@imf.au.dk>  writes:
>
>> open T, '>', 't1.txt';
>> print T "$html\n";
>> close T;
>
> A little further experimentation shows that if I change to the
> two-argument form "open T, '>t1.txt';" I do get UTF-8.

        When open() is given an explicit list of layers (with the
three-arg syntax), they override the list declared using this pragma.

from the documentation. try >:utf8, because presumably, perl treats any
three-arg as having specified the layers.
-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


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

Date: Sun, 24 Jan 2010 21:42:39 +0100
From: Rasmus Villemoes <burner+usenet@imf.au.dk>
Subject: Re: open pragma appears to have no effect
Message-Id: <u0l636r1bgg.fsf@orc10.imf.au.dk>

sreservoir <sreservoir@gmail.com> writes:

> On 1/24/2010 9:02 AM, Rasmus Villemoes wrote:
>> Rasmus Villemoes<burner@imf.au.dk>  writes:
>>
>>> open T, '>', 't1.txt';
>>> print T "$html\n";
>>> close T;
>>
>> A little further experimentation shows that if I change to the
>> two-argument form "open T, '>t1.txt';" I do get UTF-8.
>
>         When open() is given an explicit list of layers (with the
> three-arg syntax), they override the list declared using this pragma.
>
> from the documentation.

I can't find that in my "perldoc -f open", the closest is this remark

    (Note that if layers are specified in the three-arg form then
     default layers set by the "open" pragma are ignored.)

> try >:utf8, because presumably, perl treats any three-arg as having
> specified the layers.

But is '>' "an explicit list of layers"? I would assume that when no
layer is specified, some default is used. Why is that default not
taken from the open pragma? In other words, why does

  open T, '>', 'file.txt'

and

  open T, '>file.txt'

behave differently? perldoc open says "When open() is given an
explicit list of layers they are appended to the list declared using
this pragma.", and I would say that '>' specifies an empty list of
layers.

-- 
Rasmus Villemoes
<http://rasmusvillemoes.dk/>


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

Date: Sun, 24 Jan 2010 16:19:04 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: open pragma appears to have no effect
Message-Id: <hjidgj$8j9$1@speranza.aioe.org>

On 1/24/2010 3:42 PM, Rasmus Villemoes wrote:
> sreservoir<sreservoir@gmail.com>  writes:
>
>> On 1/24/2010 9:02 AM, Rasmus Villemoes wrote:
>>> Rasmus Villemoes<burner@imf.au.dk>   writes:
>>>
>>>> open T, '>', 't1.txt';
>>>> print T "$html\n";
>>>> close T;
>>>
>>> A little further experimentation shows that if I change to the
>>> two-argument form "open T, '>t1.txt';" I do get UTF-8.
>>
>>          When open() is given an explicit list of layers (with the
>> three-arg syntax), they override the list declared using this pragma.
>>
>> from the documentation.
>
> I can't find that in my "perldoc -f open", the closest is this remark

perldoc open, not the function.

>      (Note that if layers are specified in the three-arg form then
>       default layers set by the "open" pragma are ignored.)
>
>> try>:utf8, because presumably, perl treats any three-arg as having
>> specified the layers.
>
> But is '>' "an explicit list of layers"? I would assume that when no
> layer is specified, some default is used. Why is that default not
> taken from the open pragma? In other words, why does

probably shouldn't.

>    open T, '>', 'file.txt'
>
> and
>
>    open T, '>file.txt'

why do open T, "> aaa " and open T, '>', " aaa " behave differently?

> behave differently? perldoc open says "When open() is given an
> explicit list of layers they are appended to the list declared using
> this pragma.", and I would say that '>' specifies an empty list of
> layers.

empty list is a list nonetheless.

-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


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

Date: Sun, 24 Jan 2010 18:59:01 -0800 (PST)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: substitution without affecting the input string
Message-Id: <6d6bab9c-08bd-403d-8204-c2d9d033ba6b@b2g2000yqi.googlegroups.com>

my $string = "The act sat on the mta";
$string =~ s/act/cat/;

The above command will change the content of $string. Is there a way
not to update the input string but to return me a new string.


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

Date: Sun, 24 Jan 2010 22:03:46 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: substitution without affecting the input string
Message-Id: <hjj1mv$rb9$1@speranza.aioe.org>

On 1/24/2010 9:59 PM, Peng Yu wrote:
> my $string = "The act sat on the mta";
> $string =~ s/act/cat/;
>
> The above command will change the content of $string. Is there a way
> not to update the input string but to return me a new string.

do { (my $new = "The cat sat on the mta") =~ s/mta/mat./; $new }

-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


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

Date: Sun, 24 Jan 2010 19:29:37 -0800 (PST)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: substitution without affecting the input string
Message-Id: <08674ad2-9c11-4984-b448-4d6a1c33b186@l19g2000yqb.googlegroups.com>

On Jan 24, 9:03=A0pm, sreservoir <sreserv...@gmail.com> wrote:
> On 1/24/2010 9:59 PM, Peng Yu wrote:
>
> > my $string =3D "The act sat on the mta";
> > $string =3D~ s/act/cat/;
>
> > The above command will change the content of $string. Is there a way
> > not to update the input string but to return me a new string.
>
> do { (my $new =3D "The cat sat on the mta") =3D~ s/mta/mat./; $new }

This is indeed looks awkward to me. I still want to preserve the first
line (my $string =3D "The act sat on the mta";). To preserve $string, I
have to make a copy of it (as in the following code). Is there a
better way?

my $string =3D "The act sat on the mta";# I don't want to change this
line
$newstring=3D$string
$newstring =3D~ s/act/cat/;#Is there a better way for the following two
lines?


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

Date: Sun, 24 Jan 2010 21:45:08 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: substitution without affecting the input string
Message-Id: <slrnhlq4nl.2g3.tadmc@tadbox.sbcglobal.net>

Peng Yu <pengyu.ut@gmail.com> wrote:
> my $string = "The act sat on the mta";
> $string =~ s/act/cat/;
>
> The above command will change the content of $string. Is there a way
> not to update the input string but to return me a new string.


   (my $newstring = $string) =~ s/act/cat/;


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"


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

Date: Sun, 24 Jan 2010 22:50:25 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: substitution without affecting the input string
Message-Id: <hjj4eh$id$1@speranza.aioe.org>

On 1/24/2010 10:29 PM, Peng Yu wrote:
> On Jan 24, 9:03 pm, sreservoir<sreserv...@gmail.com>  wrote:
>> On 1/24/2010 9:59 PM, Peng Yu wrote:
>>
>>> my $string = "The act sat on the mta";
>>> $string =~ s/act/cat/;
>>
>>> The above command will change the content of $string. Is there a way
>>> not to update the input string but to return me a new string.
>>
>> do { (my $new = "The cat sat on the mta") =~ s/mta/mat./; $new }
>
> This is indeed looks awkward to me. I still want to preserve the first
> line (my $string = "The act sat on the mta";). To preserve $string, I
> have to make a copy of it (as in the following code). Is there a
> better way?
>
> my $string = "The act sat on the mta";# I don't want to change this
> line
> $newstring=$string
> $newstring =~ s/act/cat/;#Is there a better way for the following two
> lines?

sub replret_e($$$) {
   my $str = $_[0];
   $str =~ s/$_[1]/$_[2]/e;
}

replret($string, qw/act/, sub { "cat" });

er. that's still awkward and not exactly efficient.

-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


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

Date: Sun, 24 Jan 2010 18:19:40 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: User-defined substitution
Message-Id: <c2cu27-ati.ln1@news.rtij.nl>

On Sun, 24 Jan 2010 13:41:20 +0100, Peter J. Holzer wrote:

> [1] Actually, on HP-UX, serial consoles and telnet sessions start with
>     erase and kill set to '#' and '@' respectively. Which is highly
>     annoying, especially if you have used one of these characters in
>     your password, so don't do that ;-).

Aha, one other mystery solved, thanks!

M4


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

Date: Sun, 24 Jan 2010 11:13:16 -0800
From: sln@netherlands.com
Subject: Re: User-defined substitution
Message-Id: <qs2pl5loh3gegmip2q4icvje82ufiodeqv@4ax.com>

On Sun, 24 Jan 2010 13:41:20 +0100, "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote:

>On 2010-01-23 19:58, sln@netherlands.com <sln@netherlands.com> wrote:
>> On Sat, 23 Jan 2010 13:31:55 +0100, "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote:
>>>On 2010-01-22 17:54, sln@netherlands.com <sln@netherlands.com> wrote:
>>>> On Fri, 22 Jan 2010 16:01:25 +0100, "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote:
[snip]
>> Pattern and replacement \\ can get ugly in strings, especially the
>> even odd situation.
>>
>> Its not like:
>>   $repl = '\\\$1\$1';
>>   $str =~ s/c/$repl/;
>>     vs
>>   $str =~ s/c/\\\$1\$1/;
>
>This is a property of the Perl syntax. It doesn't have anything to do
>with the console.
>
Yes, a property of Perl parsing. Passed as arguments I guess you only
have to deal with delimeters specific to that cli, so \\\$1\$1 as argument
will be Perl internal string representation if no chars are cli delimeters.
Windows cmd processor has a wierd mix of space and dbl quote delimeters.
Its always possible to get the values interractively after the program
starts. So yeah, the console is just a device.

Well done, it looks good.

-sln


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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 2787
***************************************


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