Newbie: Difference between $array[0] and @array[0]

Discussion in 'Perl Misc' started by darren_uk, Oct 31, 2003.

  1. darren_uk

    darren_uk Guest

    I'm completely new to Perl.

    I've just read an introduction to arrays (in man perlintro).

    It explains that to get a single value from an array, use the scalar prefix
    "$"

    E.g.,

    $array[0]

    Later it explains that to get multiple values, use the array prefix "@"

    E.g.,

    @array[0,1]

    Is there any difference between:

    $array[0]
    and
    @array[0]
     
    darren_uk, Oct 31, 2003
    #1
    1. Advertising

  2. On Fri, 31 Oct 2003, darren_uk wrote:

    >Is there any difference between:
    >
    >$array[0]
    >and
    >@array[0]


    Yes. As the docs explain, @foo[$x,$y,$z] is the same as

    ($foo[$x], $foo[$y], $foo[$z])

    SPECIFICALLY, it generates a LIST of array elements. The fact that it is
    a list is important.

    $size = @array; # the number of elements in @array is put in $size
    ($first) = @array; # the first element in @array is put in $first

    If you write:

    $lines[$x] = <FILE>;

    you read ONE line from FILE, and put it in $lines[$x]. If you write

    @lines[$x] = <FILE>;

    you read ALL the (remaining) lines from FILE, and store the first in
    $lines[$x].

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Oct 31, 2003
    #2
    1. Advertising

  3. darren_uk <> wrote:
    > Is there any difference between:
    >
    > $array[0]
    > and
    > @array[0]


    The one-element-slice will give you this warning.

    Scalar value @array[0] better written as $array[0]

    And perldiag explains:

    (W syntax) You've used an array slice (indicated by @) to select a
    single element of an array. Generally it's better to ask for a scalar
    value (indicated by $). The difference is that $foo[&bar] always
    behaves like a scalar, both when assigning to it and when evaluating its
    argument, while @foo[&bar] behaves like a list when you assign to it,
    and provides a list context to its subscript, which can do weird things
    if you're expecting only one subscript.

    On the other hand, if you were actually hoping to treat the array
    element as a list, you need to look into how references work, because
    Perl will not magically convert between scalars and lists for you. See
    perlref.

    It's an odd explanation, since "@foo[bar()]" won't actually generate the
    warning... but the difference between something like "@x[0] = localtime"
    and "$x[0] = localtime" is important.

    --
    Steve
     
    Steve Grazzini, Oct 31, 2003
    #3
  4. darren_uk

    Dave Oswald Guest

    "darren_uk" <> wrote:
    > Is there any difference between:
    >
    > $array[0]
    > and
    > @array[0]


    The first equates to the single scalar element whos value is stored in
    $array[0]. The second equates to a list that holds the single value
    contained in $array[0].

    Consider the following examples:

    use strict;

    sub context {
    if ( wantarray ) {
    print "List context.\n";
    return 1;
    } else {
    print "Scalar context.\n";
    return 0;
    }
    }

    my @array = qw/Bart Homer Marge Maggy Lisa/;

    @array[context()];
    $array[context()];
    @array[0] = context();
    $array[0] = context();

    The output will be:
    List context.
    Scalar context.
    List context.
    Scalar context.

    I leave it to future lessons for you to see the significance and
    ramifications of the distinction between list and scalar context.

    The idea is that @array[......] is an array slice. An array slice is a list
    of values, even if that list is only one value long. $array is an array
    element, which is a scalar that contains some value (or no value). Always,
    you should use array slices when you need a slice, and array indexing when
    you need a scalar.
     
    Dave Oswald, Nov 1, 2003
    #4
  5. darren_uk <> wrote:


    > I've just read an introduction to arrays (in man perlintro).



    See also the "Context" section in

    perldoc perldata


    > It explains that to get a single value from an array, use the scalar prefix
    > "$"


    > $array[0]



    Right.

    Dollar sign means: "one thing". (ie. a scalar)

    If you use that on the LHS of an assignment, the RHS will
    get _scalar_ context.


    > Later it explains that to get multiple values, use the array prefix "@"


    > @array[0,1]



    Right.

    At sign means: "(potentially) many things". (ie. a list)

    If you use that on the LHS of an assignment, the RHS will
    get _list_ context.


    > Is there any difference between:
    >
    > $array[0]
    > and
    > @array[0]



    Yes sometimes.

    No sometimes.

    The question cannot be answered without seeing how they are
    used in the surrounding code.

    If they are used in a place where context matters, then
    yes, they are different.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 1, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,234
  2. Santa
    Replies:
    1
    Views:
    1,090
    Mark A. Odell
    Jul 17, 2003
  3. Ahmad Humayun
    Replies:
    10
    Views:
    556
    Keith Thompson
    May 28, 2008
  4. PerlFAQ Server
    Replies:
    0
    Views:
    361
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    266
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page