[32473] in Perl-Users-Digest
Perl-Users Digest, Issue: 3738 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 19 06:09:24 2012
Date: Thu, 19 Jul 2012 03:09:03 -0700 (PDT)
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, 19 Jul 2012 Volume: 11 Number: 3738
Today's topics:
Re: easiest way to set $1 $2 $3... <jurgenex@hotmail.com>
Re: easiest way to set $1 $2 $3... (hymie!)
Re: easiest way to set $1 $2 $3... <rweikusat@mssgmbh.com>
Re: easiest way to set $1 $2 $3... (Tim McDaniel)
Re: Regex: match double OR single quote <rweikusat@mssgmbh.com>
Re: Regex: match double OR single quote <ben@morrow.me.uk>
Re: Stupid regex problem, s/// catching extra letter <rweikusat@mssgmbh.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 18 Jul 2012 03:44:41 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: easiest way to set $1 $2 $3...
Message-Id: <gf3d08d7agdb9jpol3h27om4uln95pjspv@4ax.com>
jidanni@jidanni.org wrote:
>In /bin/sh it merely takes a
>$ set a b c
>to set $1 $2 $3.
>
>So what is the easiest way to do the same in perl?
In Perl you would say: "Whenever you find yourself enumerating your
variables with numbers, then maybe it's a good time to think about using
an array instead". So take @foo and use
@foo = (a, b, c);
>Yes in perl they are related to regexps. No don't ask me why I want to
>set them,
Sorry, but that question is at the core of your problem. For all
practical purposes you should consider those variables read-only. Yes,
technically you can assign to them, but whenever you feel the urge to do
so normally there is a different, much better way.
>Just pretend I need to use them on the next line and want to
>try some different values.
Then why don't you use different variables like @foo[1], @foo[2], ... in
this next line?
>If it takes more than just a one-liner, then perl has problems.
-v, please
>$ perl -wle '"abc" =~ /(.)(.)(.)/; print $1, $2, $3;'
>abc
>
>Big drag.
>
>So we see on perlvar there is no array that can give us even read-only
>access to
> $<digits> ($1, $2, ...)
Why would you want to do that? The only time those variables are being
set is during a m// or s/// operation. And if you really want that list
of captured groups then you can easily capture your imaginary $<digit>
array:
Matching in list context
If the "/g" option is not used, "m//" in list context returns a
list consisting of the subexpressions matched by the parentheses
in the pattern, i.e., ($1, $2, $3...).
>not of course even to think of an easy way to set them by all directly by hand
>if we need to.
Why would you want to set them manually in the first place unless you
have some very unusual needs, e.g. interpreter-level debugging or
something like that?
>Well maybe perlvar should mention what the best way so-far to access
>them all (like /bin/sh's $@, $*), and set them all (like /bin/sh's set) is!
What for?
jue
------------------------------
Date: 18 Jul 2012 12:38:25 GMT
From: hymie@lactose.homelinux.net (hymie!)
Subject: Re: easiest way to set $1 $2 $3...
Message-Id: <5006ae41$0$4723$882e7ee2@usenet-news.net>
In our last episode, the evil Dr. Lacto had captured our hero,
jidanni@jidanni.org, who said:
>In /bin/sh it merely takes a
>$ set a b c
>to set $1 $2 $3.
>
>So what is the easiest way to do the same in perl?
Um... you *do* understand that "the shell" and "a perl program" are
two completely different things, right? I mean, $1 $2 $3 in the context
of the shell, and $1 $2 $3 in the context of a running perl program
have no relation to each other beyond the fact that they are composed of
two characters, a dollar sign and a number.
>Yes in perl they are related to regexps. No don't ask me why I want to
>set them, Just pretend I need to use them on the next line and want to
>try some different values.
You could (and probably should) use $q $w $e . Set them like they were
normal variables (which they are). They take no more keystrokes than
$1 $2 $3 do.
>If it takes more than just a one-liner, then perl has problems.
I would like perl to mow my lawn. The fact that it can't doesn't mean
that "perl has problems".
This is funny -- my sig generater is random, and this is the one it picked.
--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
-------------------------------------------------------------------------------
It's not Perl's fault if you don't know how to program. --Ronald J Kimball
-------------------------------------------------------------------------------
------------------------------
Date: Wed, 18 Jul 2012 14:00:43 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: easiest way to set $1 $2 $3...
Message-Id: <878vehte9w.fsf@sapphire.mobileactivedefense.com>
jidanni@jidanni.org writes:
> In /bin/sh it merely takes a
> $ set a b c
> to set $1 $2 $3.
>
> So what is the easiest way to do the same in perl?
>
> Yes in perl they are related to regexps. No don't ask me why I want to
> set them, Just pretend I need to use them on the next line and want to
> try some different values.
Trivially, you don't "need to use them" except if capturing parts of a
string matched by some regexp. So, why don't you just use a suitable
string as input?
------------------------------
Date: Wed, 18 Jul 2012 16:16:39 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: easiest way to set $1 $2 $3...
Message-Id: <ju6nh7$jou$1@reader1.panix.com>
>>So we see on perlvar there is no array that can give us even
>>read-only access to
>> $<digits> ($1, $2, ...)
You can muck about with the symbol table, but I agree with the other
answers:
Perl: you're trying to do it wrong.
In article <gf3d08d7agdb9jpol3h27om4uln95pjspv@4ax.com>,
J_rgen Exner <jurgenex@hotmail.com> wrote:
>Then why don't you use different variables like @foo[1], @foo[2],
>... in this next line?
$foo[1], $foo[2], ...
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Wed, 18 Jul 2012 13:26:17 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Regex: match double OR single quote
Message-Id: <877gu1ntli.fsf@sapphire.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
[...]
>> Then, I thought that * meant "0 or more times", which would essentially
>> make it optional?
>
> Well, yes, in a sense. 'Optional' is ambiguous; in this case, as I said,
> I believe you want 0-or-1-times rather than 0-or-more-times.
I don't think so. ? is equivalent to the quantifier {0,1}, * is
equivalent to the quantifier {0,}. Both imply that the re they apply
to is optional for success of the match (not matching it at all is
fine). The difference is on the right side of the comma: The first one
may match at most once, the second one represents an unbounded
sequence.
------------------------------
Date: Wed, 18 Jul 2012 15:10:18 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Regex: match double OR single quote
Message-Id: <ansid9-ki71.ln1@anubis.morrow.me.uk>
Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
> Ben Morrow <ben@morrow.me.uk> writes:
>
[I recommended the OP use ? instead of *]
>
> >> Then, I thought that * meant "0 or more times", which would essentially
> >> make it optional?
> >
> > Well, yes, in a sense. 'Optional' is ambiguous; in this case, as I said,
> > I believe you want 0-or-1-times rather than 0-or-more-times.
>
> I don't think so. ? is equivalent to the quantifier {0,1}, * is
> equivalent to the quantifier {0,}. Both imply that the re they apply
> to is optional for success of the match (not matching it at all is
> fine). The difference is on the right side of the comma: The first one
> may match at most once, the second one represents an unbounded
> sequence.
I believe you are agreeing with me.
Ben
------------------------------
Date: Wed, 18 Jul 2012 13:30:56 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Stupid regex problem, s/// catching extra letter
Message-Id: <87394pntdr.fsf@sapphire.mobileactivedefense.com>
Jason C <jwcarlton@gmail.com> writes:
> On Wednesday, July 18, 2012 12:57:00 AM UTC-4, thepoet wrote:
>> What you're trying to do is a zero width negative look-behind
>> assertion.
>> s#(?<!http://)www\.#http://www.#gi should do the trick.
>> The "(?<!...)" tells the regex engine to only match the following
>> pattern if it is not preceded by the pattern in the look-behind,
>> without capturing anything.
>>
>> "perldoc perlre" has good explanations for character classes
>> and look-around assertions.
>>
>> -Chris
>
> Thanks for the help, Chris. Character classes aren't exactly
> intuitive when a symbol changes definition completely based on
> context, so I'm still struggling with that a little.
A character class denotes an unordered set of characters, meaning
[^http://]
[^htp:/]
[^:pppppth/]
[^:/hpt]
[^h:t/p]
all represent identical sets and they all match a single character.
But you wanted to match the string http:// and a regex matching a
string is just the string itself, IOW, THIS sequence of characters.
------------------------------
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 3738
***************************************