Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by ack (Deacon)
on Apr 08, 2009 at 21:46 UTC ( [id://756469]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: What is the difference between $array[1] and @array[1]?
by JavaFan (Canon) on Apr 09, 2009 at 14:07 UTC
    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.

Re^3: What is the difference between $array[1] and @array[1]?
by jethro (Monsignor) on Apr 09, 2009 at 13:27 UTC
    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://756469]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2025-01-15 06:07 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.