Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

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

G'day truthseeker66,

++toolic's answers are spot on. Here's some additional information.

"When do I use 'my' before an array and when don't I?"

While you're focussing on arrays with this specific question, the information provided applies equally to declaring other types of variables, e.g. my $scalar, my @array and my %hash.

So, as already stated, you declare your variable once then subsequently use it as many times as you want (without further declaration). If you declare a variable more than once you'll usually get a warning like this:

$ perl -Mstrict -Mwarnings -e ' my $x; my $x; ' "my" variable $x masks earlier declaration in same scope at -e line 3.

[Advanced usage: there are ways to declare variables with the same name more than once within the same piece of code - that's rarely, if ever, needed and best avoided as it's confusing and will often lead to unexpected errors.]

my is the most frequently used way to declare variables - more in-depth information can be found in perlsub (particularly under Private Variables via my()).

perlsub also has a lot of information about other ways to declare variables, including local, our, constant and state.

"Does this code '{$a <=> $b}' work with only numbers?"

You can reverse the order of the sort by swapping $a and $b:

$ perl -Mstrict -Mwarnings -E ' my @numbers = (2, 1, 3); my @strings = qw{B A C}; say sort { $a <=> $b } @numbers; say sort { $b <=> $a } @numbers; say sort { $a cmp $b } @strings; say sort { $b cmp $a } @strings; ' 123 321 ABC CBA

-- Ken


In reply to Re^3: Compilation error by kcott
in thread Compilation error by truthseeker66

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (3)
    As of 2014-11-23 03:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (128 votes), past polls