basic perl array question

Discussion in 'Perl Misc' started by Tarscher, Mar 22, 2006.

  1. Tarscher

    Tarscher Guest

    Hi all,

    I have been looking for this on the net but I didn't found an answer.
    Maybee you guys can help me out?

    I m using GD to create charts and have to use the $graph->plot(\@data)
    command somewhere. The \@ interests me and I don't know why I can just
    use $ or @ ?

    Thanks in advance
    Stijn
    Tarscher, Mar 22, 2006
    #1
    1. Advertising

  2. Tarscher wrote:
    > Hi all,
    >
    > I have been looking for this on the net but I didn't found an answer.
    > Maybee you guys can help me out?
    >
    > I m using GD to create charts and have to use the $graph->plot(\@data)
    > command somewhere. The \@ interests me and I don't know why I can just
    > use $ or @ ?
    >


    This is not really a Perl array question, but you didn't know that so I
    can't fault you for that. The backslash means that it's a reference. So
    instead of passing the @data array, you are passing a reference to
    @data.
    it_says_BALLS_on_your forehead, Mar 22, 2006
    #2
    1. Advertising

  3. Tarscher wrote:
    > Hi all,
    >
    > I have been looking for this on the net but I didn't found an answer.
    > Maybee you guys can help me out?
    >
    > I m using GD to create charts and have to use the $graph->plot(\@data)
    > command somewhere. The \@ interests me and I don't know why I can just
    > use $ or @ ?


    A more complete explanation of references can be found using the
    following command:
    perldoc perlref
    it_says_BALLS_on_your forehead, Mar 22, 2006
    #3
  4. Tarscher

    Paul Lalli Guest

    Tarscher wrote:
    > I have been looking for this on the net but I didn't found an answer.
    > Maybee you guys can help me out?
    >
    > I m using GD to create charts and have to use the $graph->plot(\@data)
    > command somewhere. The \@ interests me and I don't know why I can just
    > use $ or @ ?


    Because they are vastly different things.

    @data is an array named 'data'
    $data is a scalar variable named 'data'

    Other than their names, @data and $data have NOTHING to do with one
    another. Not even a little bit.

    \@data is a reference to the array named @data. It is a single scalar
    value. It references the array @data.

    To answer your question about why you have to pass a reference to @data
    rather than @data itself, the simple answer is "That's how the module
    designer created it.". *Why* he/she created it that way is an answer I
    cannot give without digging into the module. A likely possibility is
    that the method in question wants/needs to directly modify the array
    that it wants passed in. While possible to do without references,
    this is much cleaner to do with references.

    For more information on references, please read:
    perldoc perlreftut
    perldoc perllol
    perldoc perldsc
    perldoc perlref

    For more information on subroutines and their arguments, please read:
    perldoc perlsub

    Paul Lalli
    Paul Lalli, Mar 22, 2006
    #4
  5. it_says_BALLS_on_your forehead <> wrote:
    > Tarscher wrote:



    >> The \@ interests me


    > A more complete explanation of references can be found using the
    > following command:
    > perldoc perlref



    And a less complete, but much easier to digest, explanation of
    references can be found using the following command:

    perldoc perlreftut


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 22, 2006
    #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. dpackwood
    Replies:
    3
    Views:
    1,794
  2. kelvSYC

    Basic Array Question

    kelvSYC, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    298
    Joona I Palaste
    Oct 11, 2003
  3. Engineer
    Replies:
    6
    Views:
    614
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    428
  5. Jürgen Exner

    Re: basic perl question

    Jürgen Exner, Oct 30, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    274
    Jürgen Exner
    Oct 30, 2012
Loading...

Share This Page