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: Intermediate:
Array access question

 



gooroo175
Novice

Aug 11, 2008, 11:18 AM

Post #1 of 3 (503 views)
Array access question Can't Post

Hi there,

sometimes I'm getting confused with all those curly brackets and braces. So here's my question. I've got a function that returns an 2D-array of values and I want to access the first row only.
so basically I'm looking for a way to write the following in one line.
my @ret = func();
my @line1 = @{$ret[0]};
this works but i like to have an even short form. s.th. like
my @line1 = @{func()[0]} or so.
Thanks for every help...


shawnhcorey
Enthusiast


Aug 11, 2008, 11:31 AM

Post #2 of 3 (501 views)
Re: [gooroo175] Array access question [In reply to] Can't Post


In Reply To
Hi there,

sometimes I'm getting confused with all those curly brackets and braces. So here's my question. I've got a function that returns an 2D-array of values and I want to access the first row only.
so basically I'm looking for a way to write the following in one line.
my @ret = func();
my @line1 = @{$ret[0]};
this works but i like to have an even short form. s.th. like
my @line1 = @{func()[0]} or so.
Thanks for every help...


Add parenthesis around the function call:

Code
my @line1 = @{ (func())[0] };

(The extra whitespace is so I can read it.)

You may have seen this piece of code to get the mtime of a file:

Code
my $mtime = (stat($file))[9];


__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".


gooroo175
Novice

Aug 11, 2008, 11:35 AM

Post #3 of 3 (500 views)
Re: [shawnhcorey] Array access question [In reply to] Can't Post

great. No i didn't see this code snippet before. But i tried the same with on more pair of curly braces and got a little confused about it.
Thanks very much.

 
 


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

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