Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: A Simple Question From A Simple Man

by Anonymous Monk
on Oct 11, 2012 at 04:56 UTC ( #998360=note: print w/replies, xml ) Need Help??

in reply to A Simple Question From A Simple Man

This notion of "a reference" ("arrayref" is slang for "a reference to an array," etc.) is one of the most-important concepts in Perl. A reference is a single value ... what's called a "scalar" ... yet it can refer to anything at all. For example, even though arrays are one-dimensional, every slot in it could consist of a reference to another array, thus creating a n-dimensional array, and so on. "Contexts" (array, hash, scalar, etc.), as denoted by characters "@", "$", "%" and so on, are also a very important idea.
  • Comment on Re: A Simple Question From A Simple Man

Replies are listed 'Best First'.
Re^2: A Simple Question From A Simple Man
by TinkerTantrum (Novice) on Oct 14, 2012 at 02:32 UTC

    So what you are saying (if I'm right) is that an "arrayref" can be treated as a scalar?

      No, an array reference is a scalar:

      my @array = ('Matthew', 'Mark', 'Luke', 'John'); my $array_ref = \@array; my @new_array = @{ $array_ref }; print join(', ', @new_array);

      Here, @array is an array, as indicated by the “@” sigil, and $array_ref is a scalar, as per the “$” sigil. The syntax @{ ... } takes an array reference (which must be a scalar), and dereferences it to get the array it references.

      Here is something to meditate on: In Perl, an array can contain any number of elements, but each element must be a scalar. So, to make complex data structures (like N-dimensional arrays or arrays of hashes) you need some way of getting non-scalars (arrays or hashes) to act as scalars so they can be elements in the larger array. Which is where references come in: they are scalars which can be used to access other things, and those other things can be non-scalars.

      In addition to the reading recommendations given above by other monks, you might want to check out the Monastery’s tutorial intro to references.

      Hope that helps,

      Athanasius <°(((><contra mundum

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998360]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-06-19 06:08 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.