Question about a variable in list-context

M

Markus Hutmacher

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
 
W

Willem

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
 
M

Markus Hutmacher

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.
 
J

Jürgen Exner

Markus Hutmacher said:
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

John W. Krahn

Willem said:
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
 
W

Willem

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top