Apropos of nothing...
In the following code fragment, what context is foo() in? @ary[0] = foo() the following code @ary= foo() obviously evaluates @foo in a list context, but in the first I'm no longer sure. -- Piers "It is a truth universally acknowledged that a language in possession of a rich syntax must be in need of a rewrite." -- Jane Austen?
Re: Apropos of nothing...
Thus it was written in the epistle of Piers Cawley, > In the following code fragment, what context is foo() in? > > @ary[0] = foo() Scalar, I would think. Just my guess, Ted -- Ted Ashton ([EMAIL PROTECTED]) | From the Tom Swifty collection: Southern Adventist University| "Multiplication before addition", said Tom, Deep thought to be found at | citing precedents. http://www.southern.edu/~ashted |
Re: Apropos of nothing...
On Thu, Dec 13, 2001 at 12:12:14PM -0500, Ted Ashton wrote: > Thus it was written in the epistle of Piers Cawley, > > In the following code fragment, what context is foo() in? > > > > @ary[0] = foo() > > Scalar, I would think. I assume that the following would make the assignment a slice and therefore put foo() in list context: @i = (0); @ary[@i] = foo(); How could one get that behavior without the intermediate array? Would one of the following work? @ary[(0)] = foo(); @ary[0,] = foo(); @ary[0,0] = foo(); @ary[0,0] = list(foo()); -- Mark J. REED<[EMAIL PROTECTED]>
RE: Apropos of nothing...
Piers Cawley: # In the following code fragment, what context is foo() in? # # @ary[0] = foo() The short answer is scalar context. The long answer is below. Note that the long answer is only the way I think of it. You may think differently. I like to think of it as 'one context'. 'Scalar' and 'list' no longer describe the whole situation. The way I see it, there are three types of context: -void context (which could just be 0 context) -N context -infinite context (which could just be Inf context) Ihe meaning of each of those should be obvious. In that case, 'scalar' context is really 'one' context. However, we can still call it scalar context if it makes you feel better. :^) (Yes, those are just my opinions. They do not necessarily reflect Larry's, Damian's or the guy in the padded cell next to mine's.) --Brent Dax [EMAIL PROTECTED] Configure pumpking for Perl 6 "Nothing important happened today." --George III of England's diary entry for 4-Jul-1776
Re: Apropos of nothing...
> In the following code fragment, what context is foo() in? > > @ary[0] = foo() Scalar context. @ary[0] is a single element of @ary. To call foo() in list context use any of the following: (@ary[0]) = foo(); # Assign @ary[0] the first element returned @(@ary[0]) = foo(); # " " "" " " @ary[@(0)] = foo(); # " " "" " " @ary[0,] = foo(); # " " "" " " @ary[[0]] = foo(); # " " "" " " @ary[0] = @(foo()); # Assign @ary[0] a ref to the elems returned @ary[0] =()= foo(); # " " " " " "" " @ary[0] = [foo()]; # " " " " " "" " Damian
Re: Apropos of nothing...
On Thu, 13 Dec 2001 12:17:44 -0500, Mark J. Reed wrote: > @i = (0); > @ary[@i] = foo(); > >How could one get that behavior without the intermediate array? Parens, likely. (@ary[0]) = foo(); -- Bart.
Re: Apropos of nothing...
On Fri, Dec 14, 2001 at 06:39:02AM +1100, Damian Conway wrote: > >> In the following code fragment, what context is foo() in? >> >> @ary[0] = foo() > > Scalar context. @ary[0] is a single element of @ary. > > To call foo() in list context use any of the following: > > (@ary[0]) = foo(); # Assign @ary[0] the first element returned > @(@ary[0]) = foo(); # " " "" " " > @ary[@(0)] = foo(); # " " "" " " > @ary[0,] = foo(); # " " "" " " > @ary[[0]] = foo(); # " " "" " " > > @ary[0] = @(foo()); # Assign @ary[0] a ref to the elems returned > @ary[0] =()= foo(); # " " " " " "" " Hm, thats a change from perl5. In perl5 that would assign the number of elements returned from foo(). Is there a good reason for this change ? Graham. > @ary[0] = [foo()]; # " " " " " "" " > > Damian
Re: Apropos of nothing...
> > @ary[0] =()= foo(); # " " " " " "" " > > Hm, thats a change from perl5. In perl5 that would assign the number of > elements returned from foo(). Is there a good reason for this change ? Firstly, Larry may have to rule on which behaviour actually *is* invoked there. Secondly, my understanding was that arrays/lists (and there will be less distinction between the two in Perl 6) will *always* enreference in scalar contexts, including here. If you want the number of elements, you'd write: @ary[0] = foo().length(); # explicit request for length @ary[0] = +[foo()]; # numerification produces length Damian
Re: Apropos of nothing...
"Brent Dax" <[EMAIL PROTECTED]> writes: > Piers Cawley: > # In the following code fragment, what context is foo() in? > # > # @ary[0] = foo() > > The short answer is scalar context. The long answer is below. Note > that the long answer is only the way I think of it. You may think > differently. > > I like to think of it as 'one context'. 'Scalar' and 'list' no longer > describe the whole situation. The way I see it, there are three types > of context: > -void context (which could just be 0 context) > -N context > -infinite context (which could just be Inf context) > > Ihe meaning of each of those should be obvious. In that case, 'scalar' > context is really 'one' context. However, we can still call it scalar > context if it makes you feel better. :^) (Yes, those are just my > opinions. They do not necessarily reflect Larry's, Damian's or the guy > in the padded cell next to mine's.) Okay. Here's the examples I threw at Dan. @ary[0] = foo() # scalar @ary[1,2] = foo() # list context @bar = 1; @ary[@bar] = foo() # ? probably list or maybe scalar... @bar = (1,2); @ary[@bar] = foo() # list? @bar is constant = 1; @ary[@bar] = foo() # We know at compile time there's only one thing in # @bar. Does that mean foo() is in a scalar context # now? sub a_scalar { 1 }; sub an_array { my @a = (1,2) } sub context { wantarray ? (1,2) : 1 } @ary[a_scalar()] = foo() # ??? @ary[an_array()] = foo() # ??? At around this point, Dan was heard to say 'Mommy, make the bad man go away!' @ary[context()] = foo() # ??? Oh yes, and what context is &context called in? And, just for laughs: $ref = [1,2]; @ary[$ref] = foo(); # probably a syntax error -- Piers "It is a truth universally acknowledged that a language in possession of a rich syntax must be in need of a rewrite." -- Jane Austen?