Brackets in scalar and array
Hello all, long time lurker, first time requester... I have a Perl exam tomorrow and came across a question that I just cannot find an answer to (past paper, this isn't cheating or homework etc.). Explain the difference between: ($test)=(@test); And $test=@test; If anybody could shed any light on this I'd be very grateful. Thanks, James.
Re: Brackets in scalar and array
Hi, you can find the answer here, http://perlmaven.com/scalar-and-list-context-in-perl Good Luck to your exam! On Thu, May 29, 2014 at 1:20 PM, James Kerwin jkerwin2...@gmail.com wrote: Hello all, long time lurker, first time requester... I have a Perl exam tomorrow and came across a question that I just cannot find an answer to (past paper, this isn't cheating or homework etc.). Explain the difference between: ($test)=(@test); And $test=@test; If anybody could shed any light on this I'd be very grateful. Thanks, James.
Re: Brackets in scalar and array
On May 29, 2014, at 1:20 PM, James Kerwin wrote: Hello all, long time lurker, first time requester... I have a Perl exam tomorrow and came across a question that I just cannot find an answer to (past paper, this isn't cheating or homework etc.). Explain the difference between: ($test)=(@test); And $test=@test; If anybody could shed any light on this I'd be very grateful. The difference is the context of the assignment: scalar or list, and how @test (or (@test)) is evaluated in that context. In the first statement ($test) = (@test), the parentheses around $test in the left-hand side (LHS) of the assignment places the evaluation of the right-hand side (RHS) in list context. In list context, with two lists on either side of the assignment operator (=), assignment is made from each element on the RHS to the corresponding element on the LHS. Therefore, the one and only element on the LHS ($test) gets assigned the value of the first element of the RHS, and $test ends up with the value of $test[0]. In the second statement, the assignment is done in scalar context, and the RHS is evaluated in scalar context. A list evaluated in scalar context returns the number of elements in the list, and $test is assigned the value ($#test+1). Note that the parentheses around @test in the first statement are irrelevant. The context is list with or without them. Try it yourself: % perl -e '@t=qw(1 2 3);$t=@t;print qq($t\n);' 3 % perl -e '@t=qw(1 2 3);($t)=@t;print qq($t\n);' 1 % perl -e '@t=qw(1 2 3);($t)=(@t);print qq($t\n);' 1 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Brackets in scalar and array
On Thu, 29 May 2014 13:36:11 -0700 Jim Gibson jimsgib...@gmail.com wrote: Try it yourself: % perl -e '@t=qw(1 2 3);$t=@t;print qq($t\n);' 3 % perl -e '@t=qw(1 2 3);($t)=@t;print qq($t\n);' 1 % perl -e '@t=qw(1 2 3);($t)=(@t);print qq($t\n);' 1 This would be clearer if you used letters: $ perl -E'@t=qw( a b c );$t=@t;say$t' 3 $ perl -E'@t=qw( a b c );($t)=@t;say$t' a $ perl -E'@t=qw( a b c );($t)=(@t);say$t' a -- Don't stop where the ink does. Shawn -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Brackets in scalar and array
It seems so obvious now. Should possibly have just tested it myself before asking... Thank you all for the explanations! On 29 May 2014 21:36, Jim Gibson jimsgib...@gmail.com wrote: On May 29, 2014, at 1:20 PM, James Kerwin wrote: Hello all, long time lurker, first time requester... I have a Perl exam tomorrow and came across a question that I just cannot find an answer to (past paper, this isn't cheating or homework etc.). Explain the difference between: ($test)=(@test); And $test=@test; If anybody could shed any light on this I'd be very grateful. The difference is the context of the assignment: scalar or list, and how @test (or (@test)) is evaluated in that context. In the first statement ($test) = (@test), the parentheses around $test in the left-hand side (LHS) of the assignment places the evaluation of the right-hand side (RHS) in list context. In list context, with two lists on either side of the assignment operator (=), assignment is made from each element on the RHS to the corresponding element on the LHS. Therefore, the one and only element on the LHS ($test) gets assigned the value of the first element of the RHS, and $test ends up with the value of $test[0]. In the second statement, the assignment is done in scalar context, and the RHS is evaluated in scalar context. A list evaluated in scalar context returns the number of elements in the list, and $test is assigned the value ($#test+1). Note that the parentheses around @test in the first statement are irrelevant. The context is list with or without them. Try it yourself: % perl -e '@t=qw(1 2 3);$t=@t;print qq($t\n);' 3 % perl -e '@t=qw(1 2 3);($t)=@t;print qq($t\n);' 1 % perl -e '@t=qw(1 2 3);($t)=(@t);print qq($t\n);' 1
Re: Brackets in scalar and array
On Thu, May 29, 2014 at 3:20 PM, James Kerwin jkerwin2...@gmail.com wrote: Explain the difference between: ($test)=(@test); And $test=@test; Parens on the left make it a list context, parens on the right make it a list. Bare scalar on the left make it scalar context, bare array assigned in a scalar context return a scalar - that is, the number of elements. So, in the first, @test gets unrolled to a list of elements and assigned in list context, the first element gets assigned to the first list element in the LHS list, i.e. $test - as if $test = $test[0]; Note, in this case, the parens are optional on the RHS, as the same thing would happen in ($test) = @test; you can add to that LHS list my ($test1, $test2, $test3, @test4) = @test; and elements 0, 1, 2 go into the scalars and the rest is slurped into @test4. If there not enough elements, they get undef. An array in scalar context returns it's element count, so you can do: if ( @test == 4 ) { print There's 4 things in \@test!\n; } -- a Andy Bach, afb...@gmail.com 608 658-1890 cell 608 261-5738 wk
Re: Brackets in scalar and array
I am also a long-time lurker / First time responder, so hopefully I'll answer acceptably per the email-list conventions... I will assume that some of the basic references are available to you (or that others will cite them correctly... as they appear to be doing; many responses so far.) Anyway, *syntax* is relatively easy to answer; *why* someone might have written such a thing, not always as much... I would personally interpret this question in the way I would normally see it: Why is someone putting parentheses around these variables? When confronted with questions like this (and I often encounter obscure Perl in my daily work, so that's fairly common here) I start by directing the questioner to two things: google site:perlmonks.org parentheses variable google site:stackoverflow.com parentheses variable My justification for this is, when trying to figure out why one piece of code is a certain way, it helps to go to the sites where people who write code share what they're doing, and other people comment knowledgeably about it. (I'm sure there are other sites that others recommend; these are just the two I've found most useful.) Scanning through the results, I might use them to answer someone who asked the question the way I wrote it... more-or-less this way: Left-hand-side: Putting parentheses around a scalar (or a set of scalars) is a way to assign values to everything in the list all at the same time (even if it's just have one thing getting one assignment). Right-hand-side: Putting parentheses around an array is shorthand for getting the first element of the array (by converting the elements into a list -- changing from scalar to list context) ...in the same way that leaving the parentheses off and not specifying a single element, will return the number of elements in the array. Further, it's worth noting that putting parentheses around a single scalar on the left-hand-side of an assignment (a list of one thing)... is subtle. For example, in http://stackoverflow.com/questions/10031455/perl-using-my-with-parentheses-and-only-one-variable I see that http://stackoverflow.com/users/2766176/brian-d-foy (who answers these types of questions for a living) is essentially saying on one hand that he does it just because he's used to typing my ( and some stuff, and then ); and that just having one thing isn't a problem; but he also notes farther down http://perldoc.perl.org/perlfaq4.html#What-is-the-difference-between-a-list-and-an-array%3f for completeness. Hope this helps! -Steve Kaftanski, MadMongers.org (Madison.pm Wisconsin). On Thu, May 29, 2014 at 3:20 PM, James Kerwin jkerwin2...@gmail.com wrote: Hello all, long time lurker, first time requester... I have a Perl exam tomorrow and came across a question that I just cannot find an answer to (past paper, this isn't cheating or homework etc.). Explain the difference between: ($test)=(@test); And $test=@test; If anybody could shed any light on this I'd be very grateful. Thanks, James.
Re: Brackets in scalar and array
On Thu, 29 May 2014 16:27:36 -0500 Steve Kaftanski skaftan...@gmail.com wrote: Hope this helps! -Steve Kaftanski, MadMongers.org (Madison.pm Wisconsin). Here are some links you might find useful: * official site http://www.perl.org/ * beginners' help http://learn.perl.org/faq/beginners.html * advance help http://perlmonks.org/ * documentation http://perldoc.perl.org/ * news http://perlsphere.net/ * repository http://www.cpan.org/ * blog http://blogs.perl.org/ * regional groups http://www.pm.org/ -- Don't stop where the ink does. Shawn -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Brackets in scalar and array
Maybe I'm missing the point but isn't the following code the problem's answer? Please let me know if I am off base. Thank you; Sherman #!/usr/bin/perl my @test = a b c; ($test)=(@test); print \$test: $test\n; @test = a b c; $test = @test; print \$test: $test\n; # output lines after running this file # $test: a b c # $test: 1 On Thu, May 29, 2014 at 2:45 PM, Shawn H Corey shawnhco...@gmail.com wrote: On Thu, 29 May 2014 13:36:11 -0700 Jim Gibson jimsgib...@gmail.com wrote: Try it yourself: % perl -e '@t=qw(1 2 3);$t=@t;print qq($t\n);' 3 % perl -e '@t=qw(1 2 3);($t)=@t;print qq($t\n);' 1 % perl -e '@t=qw(1 2 3);($t)=(@t);print qq($t\n);' 1 This would be clearer if you used letters: $ perl -E'@t=qw( a b c );$t=@t;say$t' 3 $ perl -E'@t=qw( a b c );($t)=@t;say$t' a $ perl -E'@t=qw( a b c );($t)=(@t);say$t' a -- Don't stop where the ink does. Shawn -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Brackets in scalar and array
On May 29, 2014, at 3:34 PM, Sherman Willden wrote: Maybe I'm missing the point but isn't the following code the problem's answer? Please let me know if I am off base. Just a bit off (see below). #!/usr/bin/perl my @test = a b c; That is a scalar on the right-hand side. You end up with a one-element array in which the first and only element is the string 'a b c'. Shawn and I were using the qw() operator, which splits a string on whitespace and returns a list: my @test = qw(a b c); which is equivalent to and shorter than: my @test = ( 'a', 'b', 'c'); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Brackets in scalar and array
On Thu, 29 May 2014 16:02:44 -0700 Jim Gibson jimsgib...@gmail.com wrote: On May 29, 2014, at 3:34 PM, Sherman Willden wrote: Maybe I'm missing the point but isn't the following code the problem's answer? Please let me know if I am off base. Just a bit off (see below). #!/usr/bin/perl my @test = a b c; That is a scalar on the right-hand side. You end up with a one-element array in which the first and only element is the string 'a b c'. Shawn and I were using the qw() operator, which splits a string on whitespace and returns a list: my @test = qw(a b c); which is equivalent to and shorter than: my @test = ( 'a', 'b', 'c'); See http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators -- Don't stop where the ink does. Shawn -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/