in reply to What is the difference between $array[1] and @array[1]?
>perl -we"@x = @x[1]" Scalar value @x[1] better written as $x[1] at -e line 1.
Use $ unless you mean to fetch more than one element from the array at time.
my $ele = $array[1]; # Array index my @eles = @array[1,2,3]; # Array slice
Think of it this way: The sigil indicates the kind of value being returned. If you want a scalar, use the scalar sigial ($). If you want an list, use the array sigil (@). While Perl won't care in most situations, it'll signal your intent to the next person to read your code.
|
|---|
| 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:21 UTC | |
by ikegami (Patriarch) on Apr 07, 2009 at 16:29 UTC | |
by JavaFan (Canon) on Apr 07, 2009 at 16:52 UTC | |
by ikegami (Patriarch) on Apr 07, 2009 at 17:09 UTC | |
by JavaFan (Canon) on Apr 07, 2009 at 21:21 UTC | |
| |
by Porculus (Hermit) on Apr 07, 2009 at 20:05 UTC | |
by JavaFan (Canon) on Apr 07, 2009 at 21:26 UTC |