CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
if ($#ARGV != 0) what does it suggest

 



tushar
New User

Jun 12, 2009, 12:43 AM

Post #1 of 3 (1092 views)
if ($#ARGV != 0) what does it suggest Can't Post

Team,

Below is the code I am not able to understand. What does below loop check.what does 'if ($#ARGV != 0)' mean?

if ($#ARGV != 0) {
print "Error: Must give a single file path on the command line.\n";
exit 1;
}


we are executing command "/usr/xyz.pl" on command line and it exits at above line.

command : /usr/xyz.pl

Please let me know if you have any hint.

Best Regards,

Tushar


KevinR
Veteran


Jun 12, 2009, 11:23 AM

Post #2 of 3 (1085 views)
Re: [tushar] if ($#ARGV != 0) what does it suggest [In reply to] Can't Post

When you pass arguments to a perl script from the command line they are stored in the @ARGV array. "$#name" is the numeric value of the last index of an array, $#ARGV is the value of the last index of the @ARGV array. Perl arrays start at index 0 (zero) so if the value of $#ARGV is not zero:


Code
if ($#ARGV != 0) {


it means the array has more than one element in it or none (its empty). Logically its better written like so:


Code
if (@ARGV != 1) {


Which means the length of the array is one, which is what you want. If its not one then evaluate the block and return the error.

Why you posted this in the Advanced forum though is odd. Post beginner questions in the Beginner forum please.
-------------------------------------------------


(This post was edited by KevinR on Jun 12, 2009, 11:24 AM)


tushar
New User

Jun 13, 2009, 12:38 AM

Post #3 of 3 (1072 views)
Re: [tushar] if ($#ARGV != 0) what does it suggest [In reply to] Can't Post

Thank you.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives