Question about a variable in list-context

Discussion in 'Perl Misc' started by Markus Hutmacher, Jun 22, 2012.

  1. Hello,

    in a recent thread in this group I found the following line of code:

    ($id) = $input =~ /^([^\t]+)\t/;

    I understand that ($id) means that $id is used in list-context which
    means that the part of $input which matches [^\t]+ is assigned to $id.
    I understand as well that the same line of code without the list-context
    will assign a 0 or 1 to $id depending on "matches" or "matches not".
    But I don't understand to which part of the code the list-context refers.

    In other words: what is the list in the expression
    $input =~ /^([^\t]+)\t/

    Thanks in advance

    --

    Markus
    Markus Hutmacher, Jun 22, 2012
    #1
    1. Advertising

  2. Markus Hutmacher

    Willem Guest

    Markus Hutmacher wrote:
    ) Hello,
    )
    ) in a recent thread in this group I found the following line of code:
    )
    ) ($id) = $input =~ /^([^\t]+)\t/;
    )
    ) I understand that ($id) means that $id is used in list-context which
    ) means that the part of $input which matches [^\t]+ is assigned to $id.
    ) I understand as well that the same line of code without the list-context
    ) will assign a 0 or 1 to $id depending on "matches" or "matches not".
    ) But I don't understand to which part of the code the list-context refers.
    )
    ) In other words: what is the list in the expression
    ) $input =~ /^([^\t]+)\t/

    The list-context is applied to the =~ operator, so the list is the return
    value of the =~ operator. This is then assigned to the list ($id), which
    means the first value goes into $id and the rest is ignored.

    Look at:

    @id = $input =~ /^(.)(.)(.)/;

    And try to imagine what @id contains afterwards.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Jun 22, 2012
    #2
    1. Advertising

  3. Am Fri, 22 Jun 2012 20:22:23 +0000 schrieb Willem:
    > The list-context is applied to the =~ operator, so the list is the
    > return value of the =~ operator. This is then assigned to the list
    > ($id), which means the first value goes into $id and the rest is
    > ignored.
    >
    > Look at:
    >
    > @id = $input =~ /^(.)(.)(.)/;
    >
    > And try to imagine what @id contains afterwards.
    >
    >
    > SaSW, Willem


    Well, thanks for the explanation,

    I did not see that but now I've understood.
    In your example @id would contain the variables $1, $2 and $3 which are
    assigned to the content of the brackets (.) and therefore ($id) would
    contain $1 in the above case.

    --

    Markus
    Markus Hutmacher, Jun 22, 2012
    #3
  4. Markus Hutmacher <> wrote:
    >In other words: what is the list in the expression
    >$input =~ /^([^\t]+)\t/


    It is the return value of (when used in list context)
    /^([^\t]+)\t/

    The $input =~ is irrelevant here, because it simply binds the RE to
    $input instead of to the default $_.

    jue
    Jürgen Exner, Jun 23, 2012
    #4
  5. Markus Hutmacher

    Justin C Guest

    On 2012-06-22, Willem <> wrote:
    >
    > Look at:
    >
    > @id = $input =~ /^(.)(.)(.)/;



    Eccentrica Gallumbits!


    Justin.

    --
    Justin C, by the sea.
    Justin C, Jun 25, 2012
    #5
  6. Willem wrote:
    > Markus Hutmacher wrote:
    > )
    > ) in a recent thread in this group I found the following line of code:
    > )
    > ) ($id) = $input =~ /^([^\t]+)\t/;
    > )
    > ) I understand that ($id) means that $id is used in list-context which
    > ) means that the part of $input which matches [^\t]+ is assigned to $id.
    > ) I understand as well that the same line of code without the list-context
    > ) will assign a 0 or 1 to $id depending on "matches" or "matches not".
    > ) But I don't understand to which part of the code the list-context refers.
    > )
    > ) In other words: what is the list in the expression
    > ) $input =~ /^([^\t]+)\t/
    >
    > The list-context is applied to the =~ operator, so the list is the return
    > value of the =~ operator.


    The binding operator (=~) binds the variable ($input) to the match
    operator (/^([^\t]+)\t/). It does not return a value. The value is
    returned from the match operator, specifically in this case, the
    capturing parentheses in the pattern.



    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
    John W. Krahn, Jun 25, 2012
    #6
  7. Markus Hutmacher

    Willem Guest

    John W. Krahn wrote:
    ) Willem wrote:
    )> The list-context is applied to the =~ operator, so the list is the return
    )> value of the =~ operator.
    )
    ) The binding operator (=~) binds the variable ($input) to the match
    ) operator (/^([^\t]+)\t/). It does not return a value. The value is
    ) returned from the match operator, specifically in this case, the
    ) capturing parentheses in the pattern.

    I stand corrected.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Jun 25, 2012
    #7
    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. VS_NET_DEV
    Replies:
    2
    Views:
    3,786
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    687
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    636
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    422
    www.pulpjava.com
    Nov 9, 2006
  5. Bo Lindbergh

    List context versus list context

    Bo Lindbergh, Jun 21, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    212
    Charles DeRykus
    Jun 28, 2006
Loading...

Share This Page