Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Empty List miracle(1)

by Sandy (Curate)
on Apr 29, 2010 at 13:46 UTC ( #837543=note: print w/replies, xml ) Need Help??

in reply to Empty List miracle(1)

If I understand things correctly:

qw,, in scalar context returns undef, however in list context (assigning to an array), it will set the array to an empty array.

Similarly for ().

The real issue here is the differences between returning data in list and scalar context.

So, if you substitute your print statements for these, you will get '0' printed 3 times.

my @a; print(scalar(@a=empty_list1()),"\n"); print(scalar(@a=empty_list2()),"\n"); print(scalar(@a=empty_list3()),"\n");

Replies are listed 'Best First'.
Re^2: Empty List miracle(1)
by ikegami (Pope) on Apr 30, 2010 at 14:50 UTC

    Yes, but not for the reason you're implying. You're thinking @a is evaluated in scalar context when it's not. Both operands of the list assignment (@a and empty_listX()) are evaluated in list context.

    If @a is an operand of the list assignment, what's the operand of the scalar() pseudo-operator? The list assignment. scalar() causes the list assignment to be evaluated in scalar context. In scalar context, the list assignment operator returns the number of elements returned by its right hand side operand.

    Since it doesn't matter to what you are assigning, you could just as easily have written

    print(scalar(()=empty_list1()),"\n"); # 0 print(scalar(()=empty_list2()),"\n"); # 0 print(scalar(()=empty_list3()),"\n"); # 0 print(scalar(()=(4..6) ),"\n"); # 3

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://837543]
[sierpinski]: thanks LanX that helps
[haukex]: the first index is always 0 - $#array gives you the last index in @array
[sierpinski]: gotcha, thanks
[LanX]: length = last index +1
[Eily]: yes LanX, both Metz and Nancy have a train station, it's just not the for the TGV

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-12-13 16:08 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (369 votes). Check out past polls.