Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Accessing 2D array
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

Report this thread to moderator Post Follow-up to this message
Old Post
Khairul Azmi
10-28-04 01:55 PM


RE: Accessing 2D array

>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



Report this thread to moderator Post Follow-up to this message
Old Post
Ged Murphy
10-28-04 01:56 PM


Re: Accessing 2D array
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 ab
ove:

$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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
10-28-04 01:56 PM


Re: Accessing 2D array
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>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Khairul Azmi
10-29-04 08:55 AM


Re: Accessing 2D array
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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
10-29-04 08:55 AM


Re: Accessing 2D array
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 singl
e
> 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 fo
r
> 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>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Khairul Azmi
10-29-04 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:05 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.