Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

A Simple Question From A Simple Man

by TinkerTantrum (Novice)
on Oct 10, 2012 at 22:28 UTC ( #998332=perlquestion: print w/ replies, xml ) Need Help??
TinkerTantrum has asked for the wisdom of the Perl Monks concerning the following question:

Hello Fellow Monks! First of all let me say that it is an honor to be accepted into the Monastary. As an Initiate Programmer I have chosen Perl as my 1st language to learn. I could probably figure out the answer to this question myself due to the superior documentation of Perl on Linux, but I would rather get some interaction going with my fellow Monks. Anyway, this is my code:

use warnings; $rocks = [1..23]; @rocks = qw/hail slate poop drop/; print $rocks;

Here's the output:

ARRAY(0x1434d80)

My question is:What is this doing? And why?

Comment on A Simple Question From A Simple Man
Select or Download Code
Re: A Simple Question From A Simple Man
by Anonymous Monk on Oct 10, 2012 at 23:13 UTC
Re: A Simple Question From A Simple Man
by kcott (Abbot) on Oct 10, 2012 at 23:19 UTC

    G'day TinkerTantrum,

    Welcome to the monastery.

    In your code, $rocks and @rocks are two entirely different variables. Variables with a $ are scalar values; those with an @ are arrays.

    When you use brackets ([]) in the manner shown in your code, you are creating a reference to an array (typically just called an arrayref) - that's what ARRAY(0x1434d80) is referring to. You can also get a reference to an array by putting a backslash in front of the array name (e.g. \@array_name).

    You can access elements of an array with this syntax: $array_name[index] (where index is an integer). You can access elements of an arrayref with this syntax: $array_ref->[index] (again, index is an integer).

    Here's an extension to the code you posted showing all of these:

    $ perl -le ' use warnings; $rocks = [1..23]; @rocks = qw/hail slate poop drop/; print $rocks; print $rocks->[0]; print \@rocks; print $rocks[0]; ' ARRAY(0x7fe363803ae8) 1 ARRAY(0x7fe36382b108) hail

    Have a read through these documentation pages for a more in-depth description: perlintro, perldata and perlref.

    You should also declare your variables. my is the most frequently used way of doing this - perlsub has a lot more information about this and other ways to declare variables.

    In addition to use warnings;, you should also add use strict; to the start of your code: this is one of the first things you'll read about in perlintro; details can be found in strict.

    Finally, if you just want to "get some interaction going with my fellow Monks", you can use the Chatterbox (typically referred to as the CB) which should be on the panel to your right.

    -- Ken

      Aha! It all makes sense now. Thank you for the insight Mr. kcott. I must return to the dojo and meditate on this. You have been a true help!

Re: A Simple Question From A Simple Man
by Anonymous Monk on Oct 11, 2012 at 04:56 UTC
    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.

      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998332]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-10-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (148 votes), past polls