Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

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

Be careful about thinking that @ARGV is the number of members in the array. It can have a variety of meanings. Unless you are certain you understand the notion of context in Perl and how it affects the interpretation of @SOME_ARRAY it is best to be very explicit and use scalar @SOME_ARRAY instead. This will always be the number of members, whereas @SOME_ARRAY can have a variety of meanings depending on context.

Here is a brief summary of the different sigals and contexts that affect arrays in Perl:

  • scalar @SOME_ARRAY is always the number of elements in an array
  • $#SOME_ARRAY is always the final index of the array. It is usually one less than the number of elements of the array, but that is only true if the starting index of the array is 0 (the default in Perl). If you do funky things that change the starting index of the array, it is always starting index + number of elements - 1. If the starting element is 0 and your array is empty, then it will be -1. If it has members it will be one less than the number of members.

    However, if you were to use Perl's option to change the starting index of arrays and set the starting index to 100 instead of 0, then it would always be 99 greater than the number of elements in the array!

    By the way, in case you are motivated to look up how to change the starting index, don't. Changing the starting index is not a good idea. It was in vogue several years ago until people discovered all the problems it caused.

  • @SOME_ARRAY - the actual array, with a variety of meanings depending on context. In string context it is a string containing a list of members. In a numeric or boolean context it is the number of elements in the array. In an assignment context it is a data structure to hold data and in a list context it is a list of members to do something with: sort, join, insert into another list, process one by one via a foreach loop, transform via map and so on.

Here are some examples of different contexts:

  • @SOME_ARRAY=("apples","oranges","banannas") on the left side of an assignment is the actual array as a container for data, in this case the three strings "apples", "oranges", and "banannas".
  • foreach (@SOME_ARRAY) { ... } uses the array as a list of things to iterate through
  • sort @SOME_ARRAY uses the array as a list of things to sort
  • @SOME_OTHER_ARRAY=("a","b","c", @SOME_ARRAY, "z"); uses the array as a list of things to insert into @SOME_OTHER_ARRAY
  • "@SOME_ARRAY" as part of a string surrounded with double quotes converts the array members into a string containing a list of array members. You can set the delimiter between array elements to anything you like using the $" global variable.
  • 1 + @SOME_ARRAY views the array as a number, i.e. the number of elements in the array, so for a 6 element array, 1+@SOME_ARRAY is 7
  • if (!@SOME_ARRAY) also views the array as a number. It will be true if the array is non-empty (number of elements is non-0) and false if the array is empty (number of elements is 0).

In reply to Re: ARGV behaviour in getopts std by ELISHEVA
in thread ARGV behaviour in getopts std by perl_mystery

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

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (7)
    As of 2017-12-17 23:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (466 votes). Check out past polls.

      Notices?