Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

In reply to Re: A Simple Question From A Simple Man by kcott
in thread A Simple Question From A Simple Man by TinkerTantrum

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (2)
    As of 2015-11-28 07:38 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (739 votes), past polls