Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: ARGV behaviour in getopts std

by ELISHEVA (Prior)
on Dec 22, 2010 at 01:58 UTC ( [id://878398]=note: print w/replies, xml ) Need Help??


in reply to ARGV behaviour in getopts std

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).

Replies are listed 'Best First'.
Re^2: ARGV behaviour in getopts std
by ysth (Canon) on Dec 22, 2010 at 10:13 UTC
    If you do funky things that change the starting index of the array
    but don't do that.
    --
    A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |
    Online Fortune Cookie Search

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://878398]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found