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:
@ vs $

 



yankee428
New User

Jan 8, 2009, 8:38 AM

Post #1 of 5 (615 views)
@ vs $ Can't Post

i understand the following

$ is for scalar
@ is for array

what's the difference between

@ary[0]
and
$ary[0]


KevinR
Veteran


Jan 8, 2009, 9:38 AM

Post #2 of 5 (610 views)
Re: [yankee428] @ vs $ [In reply to] Can't Post


In Reply To
i understand the following

$ is for scalar
@ is for array

what's the difference between

@ary[0]
and
$ary[0]


The difference is the first one is in list context and the second one is in scalar context. The first one is also a deprecated form of the second one from earlier versions of perl. The first one can return many values:

@list = @array[1,4,3,2,6];

the second one is a scalar and can return only one value:

$value = $array[0];

Using the proper context is important as it affects the value returned depending on what you are doing or what function you are using to process a list or scalar.
-------------------------------------------------


(This post was edited by KevinR on Jan 8, 2009, 9:40 AM)


shawnhcorey
Enthusiast


Jan 8, 2009, 10:50 AM

Post #3 of 5 (606 views)
Re: [KevinR] @ vs $ [In reply to] Can't Post


In Reply To

Code
@list = @array[1,4,3,2,6];



This is called a slice. See `perldoc perldata` and search for /Slices/

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


yankee428
New User

Jan 8, 2009, 12:02 PM

Post #4 of 5 (605 views)
Re: [KevinR] @ vs $ [In reply to] Can't Post

Thanks Kevin, very helpful.


KevinR
Veteran


Jan 8, 2009, 1:40 PM

Post #5 of 5 (603 views)
Re: [shawnhcorey] @ vs $ [In reply to] Can't Post


In Reply To

In Reply To

Code
@list = @array[1,4,3,2,6];



This is called a slice. See `perldoc perldata` and search for /Slices/


Good point, it is called a list/array slice.
-------------------------------------------------

 
 


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

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