Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: So what is an array slice anyway?

by blokhead (Monsignor)
on Aug 26, 2004 at 01:38 UTC ( [id://385880]=note: print w/replies, xml ) Need Help??


in reply to So what is an array slice anyway?

First, make sure you realize that lists and arrays are different things; they return different things in scalar context. A list returns its last element, while an array returns its size.
#!/usr/bin/perl -l ## list: print scalar (4,5,6,7); ## 7 ## array: my @foo = (4,5,6,7); print scalar @foo; ## 4
Next, understand that an array (or hash) slice is simply a list (see here for one discussion). In other words, writing @foo[1,2] is like writing ($foo[1], $foo[2]) explicitly. Yes, it's a bit confusing because the @ sigil is reminiscent of an array...

Now you can see why the array slices didn't compare with == like you thought. The == operator imposes scalar context, which causes the slices to return their last element and the arrays to return their size. If it helps, scatter some print scalar @foo[0,1] statements around to see what numbers really are being compared with == in those conditionals.

blokhead

Replies are listed 'Best First'.
Re^2: So what is an array slice anyway?
by revdiablo (Prior) on Aug 26, 2004 at 05:37 UTC
    lists and arrays ... return different things in scalar context.

    Actually, there is no such thing as a list in scalar context. What you describe is the behavior of the comma operator in scalar context. There never is a list.

    Update: this is explained in perldoc -q "between a list". Incidentally, it is nearly exactly the same as what I said -- I must have subconsciously memorized the FAQ entry. :-)

    It might be interesting to note that you can see this in action with B::Deparse, e.g.:

    $ perl -MO=Deparse -e 'print scalar (2, 5, 7, 9);' print scalar('???', '???', '???', 9); -e syntax OK
Re^2: So what is an array slice anyway?
by beable (Friar) on Aug 26, 2004 at 04:42 UTC
    I know that lists and arrays are different, but as yet I haven't found in the documentation what the difference is. Where in the documentation is the difference between lists and arrays explained? I found this in perldata:
    If you evaluate an array in scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.)

    There is also a section on slices in perldata, but where does it say that a slice is a list?

      where does it say that a slice is a list?

      In a few places spread throughout perldata. Interestingly, the most direct statement on the subject is not from the section on Slices, but is a parenthetical remark in the section on Context:

      Assignment to a list (or slice, which is just a list anyway) also evaluates the righthand side in list context.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://385880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found