Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: the basic datatypes, three

by systems (Pilgrim)
on Jul 20, 2005 at 07:50 UTC ( #476400=note: print w/replies, xml ) Need Help??


in reply to the basic datatypes, three

To find the size of an array you can you can do scalar(@a) which would return 3 originally and 4 after $a[4] was set to 4;
I think you meant $a[3] and not $a[4] :)
I would also like to point at this node scalar vs list context, I think most Perl newcomers need to be introduces as soon as possible to those context issues,e.g.

What would happen if you assign a list value to a scalar variable?
And for this particular question the answer would be, the scalar get the size value of the list.
Why?
This is how Perl works, it's a ... rule.

I am commenting on this mainly to point out that it's not obvious why would scalar (@list) return the list size, one would expect a function like size @list. So it would be reasonable to justify and explain those issues sooner rather than later.
Most people like to learn why (the justification) things works in the way they do. Not just how (the rules).

Replies are listed 'Best First'.
Re^2: the basic datatypes, three
by sanPerl (Friar) on Jan 24, 2006 at 11:22 UTC
    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.
      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.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: the basic datatypes, three
by Happy-the-monk (Canon) on May 10, 2006 at 09:25 UTC

    What would happen if you assign a list value to a scalar variable?
    And for this particular question the answer would be, the scalar get the size value of the list.

    In your post, you keep mixing up list and array behaviour/context - or maybe you just misnamed "array" as "list".

    see davorgs post further below to clarify the difference.

    Cheers, Sören

      is there any possible way to find, what type is a variable of these three types.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://476400]
help
Chatterbox?
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-08-15 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (158 votes). Check out past polls.

    Notices?