Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Differ. array and List

by tphyahoo (Vicar)
on Apr 26, 2005 at 13:11 UTC ( [id://451548]=note: print w/replies, xml ) Need Help??


in reply to Differ. array and List

It's clear to me by this point that the word "list" has no real precise definition in perl, whereas the word "array" does.

But is there a difference between the terms "array context" and "list context", or are these terms used interchangably?

Ther perl function "wantarray" can distinguish between scalar and array contexts. If there is a difference between list and array context, I expect there should be an analogous way to distinguish these within code as well. Is there one?

FWIW, I did a meditation on context (scalar vs array) at Yet another List Versus Scalar Context Meditation.

Replies are listed 'Best First'.
Re^2: Differ. array and List
by tphyahoo (Vicar) on Apr 26, 2005 at 14:27 UTC
    Hope this isn't flogging a dead horse, just wanted to express this in a way that is clear to me.

    "Context" in perl means, you have a line of code with a function that returns something, ie an lvalue -- for "left value" because what gets returned is on the left side of the expression. The function can behave differently, depending on what gets returned.

    @array = myfunction(); # array or list context (same thing) $array = myfunction(); # scalar context
    If it's returning a scalar value it's scalar context, if it's returning an array value it is list context, or array context (same thing).

    So, as far as "context" is concerned, array and list are the same thing.

    But, there are places where the argument to a function has to be an array and not a list. So something like this could happen:

    functionOnlyTakesArrays(@array) # works functionOnlyTakesArrays("one","two","three") # doesn't work
    Right? Or wrong?
Re^2: Differ. array and List
by halley (Prior) on Apr 26, 2005 at 13:38 UTC
    No, there is no distinction in contexts, as far as return values from subroutines are concerned; I believe Larry Wall has admitted that the name really should be wantlist. The output is a list which can be used as-is or assigned to an array (or hash or whatever) if desired.

    However, there are some subs which expect their arguments to be array variables (because the variable's data will be altered). Examples include pop() and shift().

    --
    [ e d @ h a l l e y . c c ]

      However, there are some subs which expect their arguments to be array variables (because the variable's data will be altered). Examples include pop() and shift().

      I think this is incorrect. Any list of arguments supplied to a subroutine are inserted into the array @_ and pop and shift handle that accordingly. There's no such thing as subs expecting array variables.
        Uh, write a Perl subroutine that works exactly like pop() without (1) taking a (@) prototype signature, and (2) expecting that $_[0] is an lvalue array reference. I'd like to see the code.

        --
        [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-16 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found