Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

ARGV behaviour in getopts std

by perl_mystery (Beadle)
on Dec 21, 2010 at 20:07 UTC ( #878348=perlquestion: print w/ replies, xml ) Need Help??
perl_mystery has asked for the wisdom of the Perl Monks concerning the following question:

This is probably blindingly simple but I'm not understanding why @ARGV is not reduced to () (no args)? In this example,I am running the command as below which ends up as 6 arguments but why $#ARGV !=6 is true?

I am running the command using the following perl perl.pl -b opt_b -p opt_p -c opt_c if ($#ARGV == -1 || $#ARGV !=6) { print "ARGV is now reduced to <".@ARGV."> members:\n";#prints ARGV as +6 }

Comment on ARGV behaviour in getopts std
Download Code
Re: ARGV behaviour in getopts std
by toolic (Chancellor) on Dec 21, 2010 at 20:14 UTC
    but why $#ARGV !=6 is true?
    Because $#ARGV is 5, which you can prove by printing its value:
    use warnings; use strict; use Data::Dumper; use Getopt::Std; print '$#ARGV=', $#ARGV, "\n"; print Dumper(\@ARGV); if ($#ARGV == -1 || $#ARGV !=6) { print "ARGV is now reduced to <".@ARGV."> members:\n";#prints ARGV + as 6 } __END__ $#ARGV=5 $VAR1 = [ '-b', 'opt_b', '-p', 'opt_p', '-c', 'opt_c' ]; ARGV is now reduced to <6> members:
    @ARGV

      What is the difference between $#ARGV and @ARGV?I can see @ARGV has members,but what is $#ARGV,what does it contain?why is the number five?is it always "1" less than @ARGV?

        I might probably be simple but Can someone pls answer my query?

        You will find the answer if you follow the link that I provided in my original reply and read it. If there is something unclear in the documentation, please specify what you don't understand. Here it is again: @ARGV
        'scalar @array' is the number of elements in @array (sometimes also referred to as the length of @array).

        $#array is the last index of @array. By default the first index in an array is 0, so when using the default settings $#array will be 'scalar @array - 1'.

        Elda Taluta; Sarks Sark; Ark Arks

Re: ARGV behaviour in getopts std
by Corion (Pope) on Dec 21, 2010 at 20:15 UTC

    If @ARGV contains 6 members, what is the index of the last element?

    I prefer to avoid $#array in most cases unless I'm really talking about array indices.

    I'm not sure why Getopt::Std doesn't reduce @ARGV to an empty list, but that's because you haven't shown your usage line. Personally, I use Getopt::Long for almost everything.

Re: ARGV behaviour in getopts std
by ELISHEVA (Prior) on Dec 22, 2010 at 01:58 UTC

    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).
      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
Node Status?
node history
Node Type: perlquestion [id://878348]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-12-27 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls