in reply to Re: scalar doesn't work values returned by a function?
in thread scalar doesn't work values returned by a function?

You're right, but note that @{[ sort @a ]} evaluates as an array, not a list ($a would contain '3', not 'c').

This is probably not a problem, since ZZamboni's code doesn't seem to want the last value from the sorted list. It is, however an entirely plausible reason to try:   (To get the last sorted value) scalar(sort @array)

This is just a bit of a bummer, since it forces some kinda ugly syntax to do something which ZZamboni's code feels like it should just naturally do.

my @a = ('a','b','c'); print ((sort @a)[$#a]);
Russ

Replies are listed 'Best First'.
RE: RE: Re: scalar doesn't work values returned by a function?
by perlmonkey (Hermit) on May 10, 2000 at 08:38 UTC
    You are correct, $a would equal 3 in my last bit of code, because the sort will return a list and then store it in a referenced array which is then dereferenced and cast into scalar context which finally returns the size of the array. Got it? Good.
    I dont think I implied anything else though. I am not under the impression that ZZamboni wanted anything other than the size of the sorted array, but maybe I am wrong.

    If you wanted the last element of the sorted array of course you would do (sort @a)[-1] I certainly would not guess that scalar(sort @a) would return the last element, so I am glad that it doesnt, but apparently you think it should. I suppose you could write your own sort routine to do that in scalar context, but I like it the way it is. Just my opinion though.
      perlmonkey wrote: I certainly would not guess that scalar(sort @a) would return the last element, so I am glad that it doesnt, but apparently you think it should.

      From man perldata:

      In a context not requiring a list value, the value of the list literal is the value of the final element, as with the C comma operator. For example, @foo = ('cc', '-E', $bar); assigns the entire list value to array foo, but $foo = ('cc', '-E', $bar); assigns the value of variable bar to variable foo.
      Also:
      If you evaluate a named array in a scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value...
      From man perlfunc:
      sort LIST
      Sorts the LIST and returns the sorted list value.

      So, you see, since sort returns "the sorted list value," and lists evaluate to "the value of the final element," I would expect (at first glance) to get the last element from the list which sort returns.

      BTW, I hope I didn't offend you somehow. If so, I am terribly sorry. Your response seemed a bit hostile. Please accept my apologies...

      Just a nice guy trying to contribute,
      Russ

        Thanks for the spelling out your logic, I see now why you thought it might have been the last value. Interesting stuff. I am, apparently, not that hip on the list vs array scene.

        No I wasn't trying to be hostile, too bad tone is not available in text (without the dumb little smileys anyway). I was actually trying for a subtle humor thing, but I guess it was way to subtle.
        I appreciated your input.

        Thanks
        -Cory
        If you evaluate a named array in a scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value...

        Ahhh, this clarifies a lot. Most of the answers are in the documentation, it's just so hard to keep track of all of it...

        But it still does not explain why scalar(sort @a) returns undef. If sort @a returns a list, and a list in scalar context gets you the last value... or does it only apply to named lists?

        Thank you all for your responses. This thread has been very enlightening for me.

        --ZZamboni