-1

I know @array[0,2,6] is an array with multiple values.

And I was under the belief that $scalar=3 is a single variable with a single scalar value.

However, what is $array[3, 4]? Is it a scalar variable with two values?

Mat
  • 202,337
  • 40
  • 393
  • 406
Fighter2
  • 43
  • 5
  • Based on your comments, my book _Learning Perl_ might be useful to you. It's easier to learn the language then guess at every new thing you see. :) – brian d foy Mar 31 '12 at 01:19

2 Answers2

10

@array[0,2,6] (or more generically @array[ EXPR ]) is an array slice. (See perldata) The index expression is evaluated in list context, the returned list is taken to be a list of indexes, and the elements identified by those indexes are returned by the slice.

$array[ EXPR ] is an array element. The index expression is evaluated in scalar context, the returned value is taken to be an index, and the element identified by that index is returned.

The code 3,4 evaluates to 4 in scalar context — See the comma operator in perlop — so $array[3,4] is the same as $array[4] except for a void context warning.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Sigh. I gave a bad example to explain my problem. My problem is this. I have a code my $foo=[0x108A,0x1069]; my $obj= new Obj($filehandle,$foo); Now, I know $obj is an object to the subroutine Obj(). What is getting passed into the function after the filehandle is the question? Both values in foo? One value in foo? Is it clear now why I am confused? – Fighter2 Nov 05 '11 at 02:29
  • 2
    @Fighter2, `[ ]` creates an array and a reference to that array, and it returns the reference. This reference is what it passed to `new` in `Obj->new($filehandle, $foo)`. – ikegami Nov 05 '11 at 02:33
  • Ok. It'c clear now. Thanks! Btw, I have some more questions. Do you think I can pm you? – Fighter2 Nov 05 '11 at 02:44
  • @Fighter2, If you post them here or on PerlMonks, I will see them – ikegami Nov 05 '11 at 03:13
  • I have just posted my question. Here.http://stackoverflow.com/questions/8041853/who-wants-a-challenge-here-is-a-perl-snippet-can-someone-please-assist-me-in-u – Fighter2 Nov 07 '11 at 20:00
5

Try it to see:

perl -E '@ar = (10,11,12,42);say $ar[1,2,3]'
42

1,2,3 is a list. In scalar context, it returns its last member.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • hey friend. You still lost me. What do you mean it returns the last member? If you are trying help, you have to explain with clarity or else it will confuse others only more. Especially beginners. I understand $a[$i] and @a[$i] are different. My list is $foo=[0x108A,0x1069]; And I am passing $foo to a function. What is actually passed? The whole list OR just one value from the list?? – Fighter2 Nov 05 '11 at 02:15
  • oh and by the way, I got the error "Unrecognized switch: -E (-h will show valid options)" when I ran your example on command line. – Fighter2 Nov 05 '11 at 02:22
  • 1
    @Fighter2, `[]` creates an array and a reference to that array, and it returns the reference. This reference is what it passed to `f` in `f($foo)`. – ikegami Nov 05 '11 at 02:24
  • 1
    @Fighter2, `-E` and `say` were introduced in Perl 5.10. `perl -le'@ar = (10,11,12,42); print $ar[1,2,3]'` is roughly equivalent. – ikegami Nov 05 '11 at 02:25
  • I gave a bad example to explain my problem. My problem is this. I have a code my $foo=[0x108A,0x1069]; my $obj= new Obj($filehandle,$foo); Now, I know $obj is an object to the subroutine Obj(). What is getting passed into the function after the filehandle is the question? – Fighter2 Nov 05 '11 at 02:33