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.
If it takes more than just a one-liner, then perl has problems.
$ 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, ...)
not of course even to think of an easy way to set them by all directly by hand
if we need to.
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!
|
|
0
|
|
|
|
Reply
|
jidanni (39)
|
7/18/2012 9:25:03 AM |
|
Am 18.07.2012 11:25, schrieb jidanni@jidanni.org:
> 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.
I do ask: why would you want to do that? Just put different values in
the next line?
> If it takes more than just a one-liner, then perl has problems.
Don't understand!
> So we see on perlvar there is no array that can give us even read-only
> access to
> $<digits> ($1, $2, ...)
> not of course even to think of an easy way to set them by all directly by hand
> if we need to.
There are @+ and @- and (it is in pervar)
$1 is the same as "substr($var, $-[1], $+[1] - $-[1])"
$2 is the same as "substr($var, $-[2], $+[2] - $-[2])"
and so on.
- Wolf
|
|
0
|
|
|
|
Reply
|
NoSpamPleaseButThisIsValid3 (39)
|
7/18/2012 9:58:11 AM
|
|
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
|
|
0
|
|
|
|
Reply
|
jurgenex (445)
|
7/18/2012 10:44:41 AM
|
|
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
-------------------------------------------------------------------------------
|
|
0
|
|
|
|
Reply
|
hymie (17)
|
7/18/2012 12:38:25 PM
|
|
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?
|
|
0
|
|
|
|
Reply
|
rweikusat (2679)
|
7/18/2012 1:00:43 PM
|
|
>>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
|
|
0
|
|
|
|
Reply
|
tmcd1 (189)
|
7/18/2012 4:16:39 PM
|
|
In <ju5vf7$ah$1@news.datemas.de>, on 07/18/2012
at 05:25 PM, jidanni@jidanni.org said:
>So what is the easiest way to do the same in perl?
An assignment in array context.
>No don't ask me why I want to set them,
Why not? You're almost certainly asking the wrong question.
>If it takes more than just a one-liner, then perl has problems.
Only if it's something that programmers need to do frequently, which
it is not.
>$ perl -wle '"abc" =~ /(.)(.)(.)/; print $1, $2, $3;'
Looks normal.
>Big drag.
Why?
>So we see on perlvar there is no array that can give us even
>read-only access to
> $<digits> ($1, $2, ...)
Who cares? There's also no array to give even read-only access to $_,
$&, and $/.
>not of course even to think of an easy way to set them by all
>directly by hand if we need to.
Of course there's an easy way, but it's pointless.
>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!
If there were a point to it and it was relevant to perlvar then yes,
but since there isn't and it isn't then no.
--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>
Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org
|
|
0
|
|
|
|
Reply
|
spamtrap16 (3672)
|
7/19/2012 2:40:14 PM
|
|
>>>>> "Shmuel" == Shmuel (Seymour J ) Metz <spamtrap@library.lspace.org.invalid> writes:
Shmuel> An assignment in array context.
*list* context. "wantarray" is misnamed.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
|
|
0
|
|
|
|
Reply
|
merlyn1 (1433)
|
7/19/2012 3:45:54 PM
|
|
|
7 Replies
43 Views
(page loaded in 0.146 seconds)
|