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: Beginner:
@ARGV[$i] vs $ARG[$i]

 



DoolinDalton
Novice

Sep 25, 2009, 12:06 PM

Post #1 of 6 (990 views)
@ARGV[$i] vs $ARG[$i] Can't Post

In my program called param_test.pl, I have just one line

print @ARGV[0];

When I run this program by typing in param_test.pl hello, it outputs the word hello.

When I change the line in my program to

print $ARGV[0];

I get the same result. I'm confused on why the second one works. ARGV is an array of the command line input parameters. In short it is an array, so shouldn't it only work when I use @ in front of it?


DoolinDalton
Novice

Sep 25, 2009, 12:16 PM

Post #2 of 6 (988 views)
Re: [DoolinDalton] @ARGV[$i] vs $ARG[$i] [In reply to] Can't Post


In Reply To
In my program called param_test.pl, I have just one line

print @ARGV[0];

When I run this program by typing in param_test.pl hello, it outputs the word hello.

When I change the line in my program to

print $ARGV[0];

I get the same result. I'm confused on why the second one works. ARGV is an array of the command line input parameters. In short it is an array, so shouldn't it only work when I use @ in front of it?



Actually, I meant the other way. I expected the notation @ARV[0] to fail. When accessing an element in an array called @blah, the notation is $blah, but it seems in this example, @blah also works.


savo
User

Sep 25, 2009, 12:21 PM

Post #3 of 6 (987 views)
Re: [DoolinDalton] @ARGV[$i] vs $ARG[$i] [In reply to] Can't Post

add the below to the top of your script and it will tell you why.

use warnings;
use strict;


Scalar value @test[0] better written as $test[0]


DoolinDalton
Novice

Sep 25, 2009, 12:27 PM

Post #4 of 6 (985 views)
Re: [savo] @ARGV[$i] vs $ARG[$i] [In reply to] Can't Post


In Reply To
add the below to the top of your script and it will tell you why.

use warnings;
use strict;


Scalar value @test[0] better written as $test[0]



So, you're saying without those use statements, perl kind of figures out what I meant to say? Hmmm, good and bad, I guess.


savo
User

Sep 25, 2009, 12:38 PM

Post #5 of 6 (983 views)
Re: [DoolinDalton] @ARGV[$i] vs $ARG[$i] [In reply to] Can't Post

Even with it will carry on and just tell you

Scalar value @test[0] better written as $test[0]

but i don't know if it may cause more problems later.


FishMonger
Veteran / Moderator

Sep 25, 2009, 1:32 PM

Post #6 of 6 (979 views)
Re: [DoolinDalton] @ARGV[$i] vs $ARG[$i] [In reply to] Can't Post

@ARGV[0] is an array slice.

http://perldoc.perl.org/perldata.html#Slices

 
 


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

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