For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > getting columns of 2D array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author getting columns of 2D array
Aditi Gupta

2006-09-22, 6:57 pm

Hello Everybody,

We can get rows of a 2D array as

$aref = $AoA[$i];
print @$aref; #will print row 'i' as an array

How can I print column 'i' as an array using references? Is there any
other way which doesn't require two 'for loops' to print the columns?

Thanks in advance,
Aditi
usenet@DavidFilmer.com

2006-09-22, 6:57 pm

Aditi Gupta wrote:

> How can I print column 'i' as an array using references? Is there any
> other way which doesn't require two 'for loops' to print the columns?


Something like this should work:

my @aoa = (
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
);

print $aoa[$_][1] for 0..$#aoa; #print second column


--
David Filmer (http://DavidFilmer.com)

John W. Krahn

2006-09-22, 6:57 pm

Aditi Gupta wrote:
> Hello Everybody,


Hello,

> We can get rows of a 2D array as
>
> $aref = $AoA[$i];
> print @$aref; #will print row 'i' as an array
>
> How can I print column 'i' as an array using references? Is there any
> other way which doesn't require two 'for loops' to print the columns?


print @{ $AoA[ $i ] };



John
--
use Perl;
program
fulfillment
Bryan R Harris

2006-09-22, 9:57 pm



> Aditi Gupta wrote:
>
> Hello,
>
>
> print @{ $AoA[ $i ] };



Isn't this the same thing? He wants to print the *columns*.

It's still a loop, but you can use this:

$i = 7; # to print column 7
print map { $_->[$i-1] } @AOA;

- B



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com