Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: the basic datatypes, three

by sanPerl (Friar)
on Jan 24, 2006 at 11:22 UTC ( #525156=note: print w/ replies, xml ) Need Help??


in reply to Re: the basic datatypes, three
in thread the basic datatypes, three

Why do we need to key scalar(@list) when only @list would also work?
for e.g.

use strict; my @abc= ('a','b','c','d'); my $x= @abc; my $y = scalar(@abc); print $x."#".$y;
Here $x & $y both return 4.


Comment on Re^2: the basic datatypes, three
Select or Download Code
Re^3: the basic datatypes, three
by merlyn (Sage) on Jan 24, 2006 at 14:10 UTC
    The "scalar" op is needed only to provide scalar context in an otherwise list context space. For example:
    my @lengths = scalar @x, scalar @y, scalar @z; # but... my $length_x = @x; my $length_y = @y; my $length_z = @z;
    The scalar is needed in the first example because the array names are in a list context otherwise.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re^3: the basic datatypes, three
by perladdict (Chaplain) on May 09, 2006 at 13:12 UTC
    In this example ,assigning a list to a scalar variable returns the result as a total number of elements in a list
      assigning a list to a scalar variable returns the result as a total number of elements in a list

      No. That's not right. Evaluating a list in a scalar context (by, for example, assigning it to a scalar) returns the last item in the list. Evaluating an array in scalar context returns the number of items in the array.

      There is an important difference between a list and an array.

      # Assign a list to a scalar my $scalar = ('A', 'B', 'C'); print "$scalar\n"; # $scalar is C # Assign an array to a scalar my @array = ('A', 'B', 'C'); $scalar = @array; print "$scalar\n"; # $scalar is 3
      Update: Stupid typo fixed.
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://525156]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2015-07-02 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls