Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: What is the difference between $array[1] and @array[1]?

by morgon (Priest)
on Apr 07, 2009 at 14:21 UTC ( [id://756037]=note: print w/replies, xml ) Need Help??


in reply to What is the difference between $array[1] and @array[1]?

$array[1] is the second array element i.e. a scalar.
@array[1] is an array-slice (i.e. an array), consisting of the second array element.

(If you use slices with just one element you will get a warning though.)

But you can use a whole list in a slice like this:

my @array = qw(a b c d e); my @slice = @array[0, 2, 4]; print join "-", @slice; # this produces "a-c-e"
See "perldoc perldata".

Replies are listed 'Best First'.
Re^2: What is the difference between $array[1] and @array[1]?
by JavaFan (Canon) on Apr 07, 2009 at 16:13 UTC
    (If you use slices with just one element you will get a warning though.)

    That depends on how you write the slice:

    @a = (1 .. 5); say @a[1]; # Warning say @a[1..1]; # No warning say @a[1,]; # No warning @b = 1; say @a[@b]; # No warning
    Four one element slices, but only one warns.

    Frankly, I find the warning a bit silly; specially the warning if the slice is used in rvalue context. There isn't anything else the programmer could have reasonably meant. And in Perl6, @a[1] is going to be the required syntax anyway.

Re^2: What is the difference between $array[1] and @array[1]?
by ack (Deacon) on Apr 08, 2009 at 21:46 UTC

    Also, @array[1] drives a list context whereas $array[1] would drive scalar context. Hence:

    @array[1] = @another_array;
    will behave differently than
    $array[1] = @another_array;
    The former will put the index (i.e., the number of elements in @another_array minus one) of the last element in @another_array in the first element of @array; whereas the second will put the number of elements in @another_array in the first element of @array.

    Similarly, if you use @array1 as input to a function that behaves differently in list context vs. scalar context, the use of @array[1] will result in list context behavior, whereas $array[1] will result in scalar context behavior (hence, actually, the differences shown above in my example).

    ack Albuquerque, NM
      Similarly, if you use @array[1] as input to a function that behaves differently in list context vs. scalar context, the use of @array[1] will result in list context behavior, whereas $array[1] will result in scalar context behavior
      What kind of rubbish statement is that? The scalar vs list behaviour of a function is determined by its context, not the sigils of its arguments.

      Suppose you were right, what behaviour would func have below:

      func $foo[1], @bar[1]
      scalar, or list?

      The only difference between $arr[1] and @arr[1] lie in the cases were it can give context: lvalue context. In rvalue context, there isn't one iota of difference (which, IMO, means the warning is utterly bogus if triggered in rvalue context).

      There is a difference between $arr[EXPR] and @arr[EXPR], where EXPR isn't a scalar literal; the former gives scalar context to EXPR, the latter gives list context. But just where it makes a difference, Perl remains silent (rightly so, of course).

      Considering this is a warning about something that can be determined by a static inspection of the code (@{$arr}[1] doesn't trigger for instance), IMO, such a warning belongs in a linter. Perl::Critic for instance.

      No, the first one will put the first element of @another_array into @array.

      > perl -e '@a=(5,6,7); @array[1] = @a; print @array;' 5

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2025-01-15 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (46 votes). Check out past polls.