Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Confused as to why the "casting context" is mis-behaving

by TomDLux (Vicar)
on Oct 20, 2010 at 15:21 UTC ( #866348=note: print w/replies, xml ) Need Help??


in reply to Confused as to why the "casting context" is mis-behaving

Interesting and surprising.

DB<1> @a = qw( a b c d e ) DB<2> x @a[1,3] 0 'b' 1 'd' DB<3> x scalar @a[1,3] 0 'd' DB<4>

In my mind, the 'scalar' applies to the result of the slice, but obviously it 'reaches into' the subscripting square brackets to create a slice of the rightmost subscript.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re^2: Confused as to why the "casting context" is mis-behaving
by tinita (Parson) on Oct 20, 2010 at 15:58 UTC
    not surprising because an array slice is basically just shorter for:
    ($a[1], $a[3])
    and
    scalar ($a[1], $a[3])
    will return $a[3].
Re^2: Confused as to why the "casting context" is mis-behaving (not distributed inside)
by tye (Sage) on Oct 21, 2010 at 14:26 UTC
    In my mind, the 'scalar' applies to the result of the slice, but obviously it 'reaches into' the subscripting square brackets to create a slice of the rightmost subscript.

    That is what I wished happened. If I'd bothered to try to design what it should do rather than throw together the quickest patch possible, I would have made it do that (at least the me of today would have).

    But, no, that isn't actually what happens. You can see that easily enough:

    my @a= ('a'..'d'); my @b= (4,2,1); print scalar @a[0,@b], $/; print @a[scalar(0,@b)], $/; __END__ b # $a[1] because $b[-1] is 1 d # $a[3] because @b has 3 elements

    - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://866348]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-07-19 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (404 votes). Check out past polls.

    Notices?