Path: utzoo!attcan!uunet!lll-winken!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: @array in scalar context Keywords: array grep scalar APL Message-ID: <7200@jpl-devvax.JPL.NASA.GOV> Date: 27 Feb 90 18:03:49 GMT References: <7165@jpl-devvax.JPL.NASA.GOV> <100271@convex.convex.com> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: comp Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 42 In article <100271@convex.convex.com> tchrist@convex.COM (Tom Christiansen) writes: : The question is: what does @array do in a scalar context? Larry : proposes to set it true if the array is not empty. If this is done, : I'd like to see a warning or error if it's used in a way that this doesn't : make sense, as in: : : @foo = 7; Assigning a scalar to a list is currently legal, and the above means the same as @foo = (7); The reason it's allowed is so that local($foo) = 7; will work, since local($foo) is considered a list. : @foo =~ s/foo/bar; : if (@foo =~ /something/) The last one should be allowed, since it's conceivable that you might want to do pattern matching on the size of an array. : Of course, what I'd like better would be more work: a scalar reference : to an array is the same as the same scalar reference to each element of the : array. But along that road lies madness and APL, eh? With two ways (grep and foreach) to apply operations to array elements, I don't think we need a third way. : On arrays in scalar contexts, why doesn't this give any output: : : @a = ( 'red', 'yellow', 'green'); : print "has low\n" if grep(/low/,@a); : : If I assign the grep to @b, I get the expected element 'yellow'. Grep was returning an undefined value in a scalar context. It makes sense for it to return the number of matches though, so I have made it do that. Larry