Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Scalar context of slice

by thenaz (Beadle)
on Oct 03, 2008 at 20:22 UTC ( [id://715269]=perlquestion: print w/replies, xml ) Need Help??

thenaz has asked for the wisdom of the Perl Monks concerning the following question:

Why doesn't scalar context of a slice (as follows) give the length of the list? It seems to work if I apply a "map" that does nothing (save copy the list).
my %bar = ( a => '', b => '', ); my $broken_count = @bar{'a', 'b'}; my $working_count = map {$_} @bar{'a', 'b'}; print "broken_count : $broken_count\n"; print "working_count: $working_count\n";

Replies are listed 'Best First'.
Re: Scalar context of slice
by FunkyMonk (Chancellor) on Oct 03, 2008 at 20:30 UTC
    A slice returns a list and assigning a list to a scalar will just assign the last item in the list to the scalar. It's just the same as

    Update: A slice in scalar context returns the last item in the slice, and that will be assigned to $broken_count. It's similar in effect to

    my $foo = (1,2,3); # assigns 3 to $foo

    map in scalar context returns what you're after: the length of the list. See map.

    You may be more enlightened by the following small modification of your program:

    my %bar = ( a => 'A', b => 'B', c => 'C', d => 'D', ); my $broken_count = @bar{'a', 'b'}; my $working_count = map {$_} @bar{'a', 'b'}; print "broken_count : $broken_count\n"; # B print "working_count: $working_count\n"; # 2

    Update: Listy things in scalary context (there's no such thing as a "list in scalar context") are all special cased. You need to consult the relevant docs to see what will happen for each specific case.


    Unless I state otherwise, all my code runs with strict and warnings
      Why doesn't scalar context of a slice (as follows) give the length of the list?

      Quite simply because when I noticed that using a slice in scalar context caused Perl to core dump, I patched it to return the last item instead. Actually, that was probably only for hash slices (it was a long time ago).

      More sensibly, it is good that the simple typo/thinko of @array[$index] still produces the same value as $array[$index], not just being a silly way to write 1. What justification do you have to proclaim that the size of the slice is the best choice?

      A slice returns a list and assigning a list to a scalar will just assign the last item in the list to the scalar.

      map and grep return lists so, obviously, in a scalar context they also return the last item (according to you). Except they don't, of course.

      I'm repeatedly surprised at who next spouts this "a list in scalar context" meme that refuses to die. It is a seductive meme, but it is at least misleading (or just "wrong", if you ask me).

      Expressions that return a list of values in a list context can choose to return whatever makes the most sense when they find themselves in a scalar context. For grep the number of items makes good sense. For map, I don't think that returning the count is the best choice, but that ship sailed long, long ago. For a hash slice, dieing might have been a better choice, but that didn't even occur to me at the time I wrote that patch and it appears nobody since has felt the need to change that behavior.

      - tye        

        map and grep return lists so, obviously, in a scalar context they also return the last item (according to you). Except they don't, of course.
        Of course they don't. That's why I wrote "map in scalar context returns what you're after: the length of the list. See map."

        I really don't see what point you're trying to make tye :-(

Re: Scalar context of slice
by merlyn (Sage) on Oct 05, 2008 at 14:42 UTC
    You've fallen into the trap of thinking that there's such a thing as "a list in a scalar context". No such thing. Instead, operations recognize they are in a scalar context and return an "appropriate" thing. See my post On Scalar Context for details.
Re: Scalar context of slice
by JavaFan (Canon) on Oct 04, 2008 at 01:14 UTC
    The canonical way of getting the number of elements of the list a function returns in list context is not by using 'map' (although that will work), but by assigning the result to an empty list, and assigning that to a scalar:
    my $count = () = expression_in_list_context;
    And if you remove some spaces, you're left with one of Perls secret operators:
    my $count =()= expression; # Goatse operator.
Re: Scalar context of slice
by wol (Hermit) on Oct 06, 2008 at 14:10 UTC
    Welcome to the monastary, thenaz.

    Furthermore, congratulations on stimulating such intense discussion with such an innocuous question, and on your first post at that! I predict you'll fit in well round here :-)

    --
    .sig : File not found.

Re: Scalar context of slice
by Xilman (Hermit) on Oct 09, 2008 at 15:25 UTC

    In scalar context, @bar{'a', 'b'} is the last element of the list, '' in your example.

    The result of the map in list context is a copy of bar but, when evaluated in scalar context, gives the length of the list it copies.

    More on p76 of The Camel Book, 3rd Ed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://715269]
Approved by Fletch
Front-paged by Erez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found