Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: easiest way to set $1 $2 $3...

Reply
Thread Tools

Re: easiest way to set $1 $2 $3...

 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-18-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      07-18-2012
>>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 <(E-Mail Removed)>,
J_rgen Exner <(E-Mail Removed)> wrote:
>Then why don't you use different variables like @foo[1], @foo[2],
>... in this next line?


$foo[1], $foo[2], ...

--
Tim McDaniel, (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: easiest way to set $1 $2 $3... hymie! Perl Misc 0 07-18-2012 12:38 PM
Re: easiest way to set $1 $2 $3... Wolf Behrenhoff Perl Misc 0 07-18-2012 09:58 AM
Easiest way in Ruby to express "given this, set target to this if nilor undefined, else increment by this" RichardOnRails Ruby 4 01-23-2011 07:17 PM
Searching easiest way to convert/copy a (Hash)Set<--->List Gianni Galore Java 0 01-12-2011 02:19 PM
easiest way to set future date enspired Ruby 1 10-30-2005 03:18 PM



Advertisments