Solved

Passing 2D array to a sub()

Posted on 2008-06-20
5
237 Views
Last Modified: 2010-03-05
Hi,

I read a 2d array from file, so I have something like:

    @my2dArray[5][5];

I can iterate over it, ok. I want to pass it to a sub routine, like:

    pass2dArray(my2dArray);

    sub pass2dArray()
    {
        my (@array) = @_;
    }

Am I passing a copy of the 2d array to the function, or is it just referencing the original array?

Then, if I want to pass a single row of the array to another sub, how would I do that? I was hoping something like:

    sub pass2dArray()
    {
        my (@array) = @_;
   
        # pass row 2:
        passOneRowOf2dArray(2);
       
        # etc
        passOneRowOf2dArray(4);
    }

    sub passOneRowOf2dArray()
    {
        my (@row) = @_;
        for (my $i = 0; $i < @row; $i++) {
           ...
        }
    }
   

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 21831510
With this:
    pass2dArray(@my2dArray);
You are passing a copy of the original array, which contains references to the 2nd dimension.  You are not passing copies of the 2nd dimension.  Meaning, if your pass2dArray function, if you add or remove elements from @array, this will not be affected in @my2dArray.  But if you change, add or remove elements from any of the rows, this will be affected in @my2dArray.

To pass one row as a reference, you would use:
    passOneRowOf2dArray($array[2]);    #passes reference to second row
To pass one row as an array (not a reference), you would use:
    passOneRowOf2dArray(@{$array[2]});

If you want to pass a copy of a structure like this, you can use the Storable module's dclone function:
http://search.cpan.org/~ams/Storable-2.18/Storable.pm
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21831577
>>To pass one row as a reference, you would use:
    passOneRowOf2dArray($array[2]);    #passes reference to second row


Ok, that's just what I put, but how do you recover the reference to the single row in the function? Like:

    sub passOneRowOf2dArray()
    {
        my @row = shift;
    }

???

That doesn't work though...

Thanks

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 21831919

sub passOneRowOf2dArray {

    my $ref_to_row = shift;    #get reference

    

    #changes to $ref_to_row will show up in the original array

    print "first value=$ref_to_row->[0]\n";

    print "all values=" . join(", ", @{$ref_to_row}) . "\n";

    

    #Get as a regular array, makes a copy

    my @array_copy = @{$ref_to_row};

    

    #changes to @array_copy will not show up in the original array

    print "first value=$array_copy[0]\n";

    print "all values=" . join(", ", @array_copy) . "\n";

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 21831928
This website might be helpful:
http://perldoc.perl.org/perlreftut.html
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21834512
Perfect, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 73
execute pl/sql block from perl program 3 117
perl script help 5 232
perl match and sort unique result 2 129
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now