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:
interpreting subarray in list context

 



amuses
New User

Apr 13, 2007, 8:39 AM

Post #1 of 2 (343 views)
interpreting subarray in list context Can't Post

 
@a1 = ( 4 , 5, 6);
($x, $y, $z) = @a1[0..2];
print "$x, $y, $z\n"; #outputs 4 5 6 as expected

@a2 = ();
$a2[0] = [4, 5, 6]; # a sub-array
($x, $y, $z) = $a2[0][0..2]; # DOESNT WORK THE SAME

So what's the correct analogous syntax to do this?

And by analogous I mean, not this:
($x, $y, $z) = ($a2[0][0] , $a2[0][1], $a2[0][2]);
which works, but is not analogous.


This mystery has been illuding me for years.


KevinR
Veteran


Apr 13, 2007, 10:27 AM

Post #2 of 2 (341 views)
Re: [amuses] interpreting subarray in list context [In reply to] Can't Post


Code
@a2 = (); 
$a2[0] = [4, 5, 6];
($x, $y, $z) = @{$a2[0]};
print "$x, $y, $z";



Code
@a2 = (); 
$a2[0] = [3, 4, 5, 6, 7, 8];
($x, $y, $z) = @{$a2[0]}[1..3];
print "$x, $y, $z";

-------------------------------------------------

 
 


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

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