Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Array/List Strangeness

by Marshall (Canon)
on Aug 05, 2009 at 02:29 UTC ( [id://785953]=note: print w/replies, xml ) Need Help??


in reply to Re: Array/List Strangeness
in thread Array/List Strangeness

For a Perl @var, two thing can happen:
(a) @var is declared but does not exist in the sense that it is "empty", containing NO values, not even "undef".
(b) @var is declared and does exist in the sense that it has "actual values" or undef as one or more values (undef is a value).

my @var = (1,2,3);
@var = (); # @var is "empty", NO values
@var =undef; # @var has one value, undef

Yes, this is weird that an @var can have "nothing" in it, not even undef. This @var=() is often seen in code that "zero'es" out a @variable.

I have tried to avoid using the term "array" or "list" for these @vars. I remain curious as to when @var is an array and when @var is a list.

Update: I prefer the word "list" for Perl @vars because a list can be empty. I don't think of an array as "empty". In my thinking, an array has a null element, null pointer, etc. But the idea that it is "empty", in the same sense that a list can be "empty" (like a completely blank "to-do list) doesn't make sense to me.

Replies are listed 'Best First'.
Re^3: Array/List Strangeness
by ikegami (Patriarch) on Aug 05, 2009 at 02:58 UTC

    Again, I know *what* is happening. The question is *why*. Why does requesting an non-existing array element return undef and requesting an non-existing list element return nothing.

    I have tried to avoid using the term "array" or "list" for these @vars. I remain curious as to when @var is an array and when @var is a list.

    @var is an array. It evaluates to a list in list context, just like every other operator.

      There is no such thing as a non-existing array element. Every element of an array, which has not been set to something else, will return the value undef. Even if you only use the first 10 elements of the array, all other elements are virtually existing and hold the value undef. Also, an array is evaluated as a list in list context, but this happens after the slicing. Compare the results of the following code:
      use Data::Dumper; my @foo = qw/a b c/; my @bar = $foo[3]; print Dumper \@bar; @bar = ( @foo )[3]; print Dumper \@bar;

        There is no such thing as a non-existing array element

        Yes, there is. An array with 5 undef values in it has size 5 and not unlimited size. And concatenating two arrays works only because the first array is not unlimited

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found