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

Discussion in 'Perl Misc' started by Jürgen Exner, Jul 18, 2012.

  1. 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
     
    Jürgen Exner, Jul 18, 2012
    #1
    1. Advertising

  2. Jürgen Exner

    Tim McDaniel Guest

    >>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 <>,
    J_rgen Exner <> wrote:
    >Then why don't you use different variables like @foo[1], @foo[2],
    >... in this next line?


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

    --
    Tim McDaniel,
     
    Tim McDaniel, Jul 18, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Galore
    Replies:
    0
    Views:
    353
    Gianni Galore
    Jan 12, 2011
  2. enspired

    easiest way to set future date

    enspired, Oct 30, 2005, in forum: Ruby
    Replies:
    1
    Views:
    99
    Gene Tani
    Oct 30, 2005
  3. RichardOnRails
    Replies:
    4
    Views:
    137
    RichardOnRails
    Jan 23, 2011
  4. Wolf Behrenhoff

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

    Wolf Behrenhoff, Jul 18, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    440
    Wolf Behrenhoff
    Jul 18, 2012
  5. hymie!

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

    hymie!, Jul 18, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    384
    hymie!
    Jul 18, 2012
Loading...

Share This Page