Home > Archive > PERL Beginners > October 2004 > Accessing 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 |
Accessing 2D array
|
|
| Khairul Azmi 2004-10-28, 8:55 am |
| I've been trying to solve this problem using many techniques I found
on the websites but still unsuccessfully
sub func {
my (@local_array) = @_;
for (my $i=0;$i<@local_array;$i++) {
for (my $j=0;$j<@local_array[$i];$j++) {
print "$local_array[$i[[$j] ";
}
print "\n";
}
$myarray[0]=(1,2,3,4,5);
$myarray[1]=(6,7,8,9,10);
func (@myarray);
Thanks in advance
Azmi
| |
| Ged Murphy 2004-10-28, 8:56 am |
|
>I've been trying to solve this problem using many techniques I found
>on the websites but still unsuccessfully
>
>sub func {
> my (@local_array) = @_;
>
> for (my $i=0;$i<@local_array;$i++) {
> for (my $j=0;$j<@local_array[$i];$j++) {
> print "$local_array[$i[[$j] ";
> }
> print "\n";
> }
>
>$myarray[0]=(1,2,3,4,5);
>$myarray[1]=(6,7,8,9,10);
>
>func (@myarray);
What exactly are you trying to do?
There are quite a few things wrong with your code.
e.g. You are missing a right curly bracket at the end of your subroutine.
You are also trying to put an array of numbers into a scalar.
You are using the array inside the loop
Too many square brackets.
etc, etc.
Explain what it is you want and we will help correct your code.
****************************************
********************************
The information contained in this message or any of its
attachments is confidential and is intended for the exclusive
use of the addressee. The information may also be legally
privileged. The views expressed may not be company policy,
but the personal views of the originator. If you are not the
addressee, any disclosure, reproduction, distribution or other
dissemination or use of this communication is strictly prohibited.
If you have received this message in error, please contact
postmaster@exideuk.co.uk
<mailto:postmaster@exideuk.co.uk> and then delete this message.
Exide Technologies is an industrial and transportation battery
producer and recycler with operations in 89 countries.
Further information can be found at www.exide.com
| |
| John W. Krahn 2004-10-28, 8:56 am |
| Khairul Azmi wrote:
> I've been trying to solve this problem using many techniques I found
> on the websites but still unsuccessfully
>
> sub func {
> my (@local_array) = @_;
>
> for (my $i=0;$i<@local_array;$i++) {
> for (my $j=0;$j<@local_array[$i];$j++) {
> print "$local_array[$i[[$j] ";
> }
> print "\n";
> }
>
> $myarray[0]=(1,2,3,4,5);
> $myarray[1]=(6,7,8,9,10);
You are only assigning a single value to a scalar. This does the same as above:
$myarray[0]=5;
$myarray[1]=10;
> func (@myarray);
You want something like this:
sub func {
my @local_array = @_;
for my $array ( @local_array ) {
for my $element ( @$array ) {
print "$element ";
}
print "\n";
}
}
my @myarray = ( [ 1,2,3,4,5 ], [ 6,7,8,9,10 ] );
func( @myarray );
John
--
use Perl;
program
fulfillment
| |
| Khairul Azmi 2004-10-29, 3:55 am |
| Thanks for the solution. Not sure if this is still on topic but I then
add an additional parameter to the subroutine
sib func {
my (@local_array, $local_value) = @_;
print "$local_value \n";
for my $array ( @local_array ) {
for my $element ( @$array ) {
print "$element ";
}
print "\n";
}
}
my $value = 5;
my @myarray = ( [ 1,2,3,4,5 ], [ 6,7,8,9,10 ] );
func( @myarray, $value );
But then the for loop would return one extra empty line. Does it has
to do with the additional parameter? What if the parameter is another
array? Another 2D array may be?
Azmi
On Thu, 28 Oct 2004 02:52:04 -0700, John W. Krahn <krahnj@telus.net> wrote:
> Khairul Azmi wrote:
>
> You are only assigning a single value to a scalar. This does the same as above:
>
> $myarray[0]=5;
> $myarray[1]=10;
>
>
> You want something like this:
>
> sub func {
> my @local_array = @_;
>
> for my $array ( @local_array ) {
> for my $element ( @$array ) {
> print "$element ";
> }
> print "\n";
> }
> }
>
> my @myarray = ( [ 1,2,3,4,5 ], [ 6,7,8,9,10 ] );
>
> func( @myarray );
>
> John
> --
> use Perl;
> program
> fulfillment
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
| |
| John W. Krahn 2004-10-29, 3:55 am |
| Khairul Azmi wrote:
> Thanks for the solution. Not sure if this is still on topic but I then
> add an additional parameter to the subroutine
>
> sib func {
> my (@local_array, $local_value) = @_;
>
> print "$local_value \n";
> for my $array ( @local_array ) {
> for my $element ( @$array ) {
> print "$element ";
> }
> print "\n";
> }
> }
>
> my $value = 5;
> my @myarray = ( [ 1,2,3,4,5 ], [ 6,7,8,9,10 ] );
>
> func( @myarray, $value );
>
> But then the for loop would return one extra empty line. Does it has
> to do with the additional parameter? What if the parameter is another
> array? Another 2D array may be?
The problem is that Perl's subroutines pass all their arguments as a single
list so in your case @local_array gets everything and $local_value gets
nothing. You can reverse the order of the arguments or use a reference for
the array.
perldoc perlsub
John
--
use Perl;
program
fulfillment
| |
| Khairul Azmi 2004-10-29, 3:56 am |
| This could be it. My final question is how to do it when we have two
arrays to pass to a subroutine. I am using the techniques I came
accross in perldoc perlsub but could not figure out what should I have
on the marked line.
sub func2 {
my ($local_value, $local_array) = @_;
foreach my $value (@$local_value) {
print "$value \n";
}
for my $i (0 .. @$local_array - 1) {
for my $j (0 .. @{$local_array[$i]} - 1) { <= line
print "@$local_array[$i][$j] "; <= line
}
print "\n";
}
}
my @myarray=([1,2,3,4,5],[6,7,8,9,10],[11,1
2,12,3,4]);
my @value=(A,B,C,D,E);
func2 (\@value, \@myarray);
The above codes would only return the content of array @value.
On Thu, 28 Oct 2004 20:14:14 -0700, John W. Krahn <krahnj@telus.net> wrote:
> Khairul Azmi wrote:
>
> The problem is that Perl's subroutines pass all their arguments as a single
> list so in your case @local_array gets everything and $local_value gets
> nothing. You can reverse the order of the arguments or use a reference for
> the array.
>
> perldoc perlsub
>
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
|
|
|
|
|