Tuesday, January 3, 2012

What’s the difference between accessing an array element with $items[$index] and@items[$index]?

Using a $ retrieves the identified element, which is a scalar. @ takes an array slice from $index to the end. While both these will get your value, the difference can be important, for example:

$arr[0] = ; # reads the first line, as intended.
@arr[0] = ; # reads the whole file, "disappearing" everything after the first line.

The second is a single item array slice and is deprecated.

No comments :