Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question about a variable in list-context

Reply
Thread Tools

Question about a variable in list-context

 
 
Markus Hutmacher
Guest
Posts: n/a
 
      06-22-2012
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
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      06-22-2012
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
 
Reply With Quote
 
 
 
 
Markus Hutmacher
Guest
Posts: n/a
 
      06-22-2012
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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-22-2012
Markus Hutmacher <(E-Mail Removed)> 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
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      06-25-2012
On 2012-06-22, Willem <(E-Mail Removed)> wrote:
>
> Look at:
>
> @id = $input =~ /^(.)(.)(.)/;



Eccentrica Gallumbits!


Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-25-2012
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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      06-25-2012
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
 
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
Question: Evaluate an string variable's value to a variable Mir Nazim Python 2 12-21-2008 07:05 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
question: referencing a variable with a variable knohr Ruby 5 06-30-2007 11:51 AM
Newbie Question: Global variable vs. Top-level variable Sam Kong Ruby 2 05-25-2005 09:47 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments