Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to pass 2D array to sub function and return 2D array?

Reply
Thread Tools

How to pass 2D array to sub function and return 2D array?

 
 
Davy
Guest
Posts: n/a
 
      08-03-2006
Hi all,

I want to pass 2D array to sub function and return 2D array.

//--------------------------
For example,
use strict;
use warnings;
my @array=([1,2,3],[4,5,6],[7,8,9]);
my $array_ref = \@array;

$array_r = increase_array_element($array_ref);

sub increase_array_element {
//recover the 2D array
my @array_tmp = @{$array_ref};
print $array_tmp[2][2];
return \@array_tmp;
}
//-------------------------

But it seems the compile did not think @array_tmp is a 2D array?

Thanks!
Davy

 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      08-03-2006

"Davy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi all,
>
> I want to pass 2D array to sub function and return 2D array.
>
> //--------------------------
> For example,
> use strict;
> use warnings;
> my @array=([1,2,3],[4,5,6],[7,8,9]);
> my $array_ref = \@array;
>
> $array_r = increase_array_element($array_ref);
>
> sub increase_array_element {
> //recover the 2D array
> my @array_tmp = @{$array_ref};
> print $array_tmp[2][2];
> return \@array_tmp;
> }
> //-------------------------
>
> But it seems the compile did not think @array_tmp is a 2D array?
>


Best to copy'n'paste code. The code you have posted won't compile.

Just as inside the subroutine where you dereferenced the array reference
($array_ref) to an array (@array_temp), you could do the same with $array_r
(which is also an array reference - since that is what the subroutine
returns).

Something like:

use strict;
use warnings;
my @array=([1,2,3],[4,5,6],[7,8,9]);
my $array_ref = \@array;

my $array_r = increase_array_element($array_ref);
my @array_r_r = @{$array_r};
print $array_r_r[1][1], "\n";

sub increase_array_element {
my @array_tmp = @{$array_ref};
print $array_tmp[2][2], "\n";
return \@array_tmp;
}

Cheers,
Rob


 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-03-2006
Davy <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi all,
>
> I want to pass 2D array to sub function and return 2D array.
>
> //--------------------------


That's not a valid Perl comment. It's a fatal error.

> For example,
> use strict;
> use warnings;
> my @array=([1,2,3],[4,5,6],[7,8,9]);
> my $array_ref = \@array;
>
> $array_r = increase_array_element($array_ref);


$array_r is undeclared. That's a fatal error under "strict".

Calling "increase_array_ref" with an argument is useless because
the sub doesn't look at its arguments.

> sub increase_array_element {
> //recover the 2D array
> my @array_tmp = @{$array_ref};
> print $array_tmp[2][2];
> return \@array_tmp;
> }
> //-------------------------


See above.

> But it seems the compile did not think @array_tmp is a 2D array?


How do you know that?

Your code doesn't compile at all, you didn't even bother to run it
before posting.

Make your program runnable. If it still doesn't do what you expect,
post again, explaining what you expected and what happens instead.

Anno
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-03-2006
Sisyphus wrote:
> "Davy" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>
>
> Just as inside the subroutine where you dereferenced the array reference
> ($array_ref) to an array (@array_temp), you could do the same with $array_r
> (which is also an array reference - since that is what the subroutine
> returns).
>
> Something like:
>
> use strict;
> use warnings;
> my @array=([1,2,3],[4,5,6],[7,8,9]);
> my $array_ref = \@array;
>
> my $array_r = increase_array_element($array_ref);
> my @array_r_r = @{$array_r};
> print $array_r_r[1][1], "\n";
>
> sub increase_array_element {
> my @array_tmp = @{$array_ref};
> print $array_tmp[2][2], "\n";
> return \@array_tmp;
> }



This code contains the same error as the OP's. You are passing this
subroutine an argument, but never obtaining that argument from within
the subroutine. It will "work" simply because the argument you passed
was declared within the scope of both the function definition and the
function call, but that's not what you want to do.

sub increase_array_element {
my @array_temp = @{$_[0]};
$array_tmp[2][2]++;
print $array_tmp[2][2], "\n";
return \@array_tmp;
}

Please note that because of the way multi-dimensional structures work
in Perl, both the original and modified arrays contain references to
the *same* arrays, and so you have actually modified both $array_r and
$array_ref. . .

Paul Lalli

 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      08-03-2006

"Paul Lalli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
..
..
>
> This code contains the same error as the OP's.


The code I posted does not contain any errors.

I was aware that the OP's subroutine took no arguments. If I had also
noticed that the OP was calling the subroutine *with* an argument then I
would have amended that when I did the copy'n'paste of his code. I did not
notice the OP had done this until Anno pointed it out.

It's good that you took the time to explain how to pass the argument to the
subroutine. I ignored that aspect because (I thought) the question asked was
concerned with the sub's *return* value - but given the subject line, your
explanation is certainly warranted.

Cheers,
Rob


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-03-2006
Sisyphus wrote:
> "Paul Lalli" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> .
> .
> >
> > This code contains the same error as the OP's.

>
> The code I posted does not contain any errors.


It does not contain any errors in that it will, indeed, work exactly as
posted. However, the general methodology given by your example will
not. Passing an argument into a subroutine and later referring to that
argument in the subroutine by the name it had as an argument to the
function call is an error. Because it "works" under a specific
circumstance (the call and the definition being in the same scope) does
not make it not an error.

Paul Lalli

 
Reply With Quote
 
Davy
Guest
Posts: n/a
 
      08-04-2006
Hi Lalli,

I have used your code and passed, thanks!

Davy

Paul Lalli wrote:
> Sisyphus wrote:
> > "Davy" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) ups.com...
> >
> >
> > Just as inside the subroutine where you dereferenced the array reference
> > ($array_ref) to an array (@array_temp), you could do the same with $array_r
> > (which is also an array reference - since that is what the subroutine
> > returns).
> >
> > Something like:
> >
> > use strict;
> > use warnings;
> > my @array=([1,2,3],[4,5,6],[7,8,9]);
> > my $array_ref = \@array;
> >
> > my $array_r = increase_array_element($array_ref);
> > my @array_r_r = @{$array_r};
> > print $array_r_r[1][1], "\n";
> >
> > sub increase_array_element {
> > my @array_tmp = @{$array_ref};
> > print $array_tmp[2][2], "\n";
> > return \@array_tmp;
> > }

>
>
> This code contains the same error as the OP's. You are passing this
> subroutine an argument, but never obtaining that argument from within
> the subroutine. It will "work" simply because the argument you passed
> was declared within the scope of both the function definition and the
> function call, but that's not what you want to do.
>
> sub increase_array_element {
> my @array_temp = @{$_[0]};
> $array_tmp[2][2]++;
> print $array_tmp[2][2], "\n";
> return \@array_tmp;
> }
>
> Please note that because of the way multi-dimensional structures work
> in Perl, both the original and modified arrays contain references to
> the *same* arrays, and so you have actually modified both $array_r and
> $array_ref. . .
>
> Paul Lalli


 
Reply With Quote
 
Davy
Guest
Posts: n/a
 
      08-04-2006
No, I use Perl in my work. And I found reference in Perl is not so
clear as in C.

Anyhow, thanks.
Davy

A. Sinan Unur wrote:
> "Davy" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed) oups.com:
>
> > Hi Lalli,
> >
> > I have used your code and passed, thanks!
> >
> > Davy

>
> So, this was a homework assignment? In that case, I won't be seeing you
> again.
>
> Sinan
>
> --
> A. Sinan Unur <(E-Mail Removed)>
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:
> http://augustmail.com/~tadmc/clpmisc...uidelines.html


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-04-2006
>>>>> "D" == Davy <(E-Mail Removed)> writes:

D> No, I use Perl in my work. And I found reference in Perl is not so
D> clear as in C.

well, considering they are dramatically different, why should they be as
clear? c uses raw address pointers which can be manipulated, cast and
broken in too many ways to count. perl uses intelligent references which
can only be created by perl, can't be directly manipulated by user code
(no segfaults from perl refs) and can only be dereferenced to the
original thing that was referenced. does that sound like they are so
similar that knowing c pointers would make it clear how perl's refs
work? they have almost nothing in common.

and if you think c's pointers are clear and perl's refs are not, you
haven't done much deep data work and coding in either lang. i have done
plenty in both and i much prefer perl's refs. tracking segfaults is not
how i want to spend the rest of my life.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Davy
Guest
Posts: n/a
 
      08-04-2006
Hi,

I understand. c's pointers are too flexible to generate bugs and Perl's
reference is more strict. Thanks!

Davy


Uri Guttman wrote:
> >>>>> "D" == Davy <(E-Mail Removed)> writes:

>
> D> No, I use Perl in my work. And I found reference in Perl is not so
> D> clear as in C.
>
> well, considering they are dramatically different, why should they be as
> clear? c uses raw address pointers which can be manipulated, cast and
> broken in too many ways to count. perl uses intelligent references which
> can only be created by perl, can't be directly manipulated by user code
> (no segfaults from perl refs) and can only be dereferenced to the
> original thing that was referenced. does that sound like they are so
> similar that knowing c pointers would make it clear how perl's refs
> work? they have almost nothing in common.
>
> and if you think c's pointers are clear and perl's refs are not, you
> haven't done much deep data work and coding in either lang. i have done
> plenty in both and i much prefer perl's refs. tracking segfaults is not
> how i want to spend the rest of my life.
>
> uri
>
> --
> Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
> Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
FAQ 7.15 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}? PerlFAQ Server Perl Misc 0 01-22-2011 11:00 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
how do i pass an array to a function .. and also return an array back from the func???? bhejafry.mmk@gmail.com C Programming 3 11-30-2006 05:05 PM



Advertisments