Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem passing an array in a subroutine

Status
Not open for further replies.

user2base

Technical User
Oct 16, 2002
29
GB
I've got a program that works well. It parses a text file and, using a list of keywords and rules, it splits the text file in differents files. Each of these files contain a part of the original text file. This program uses an array that contains the original text file.

This program will be used often in my project so I decided to create a subroutine and put it in a module.

The problem is when I call this program as a subroutine from an other one, the file creation doesn't work i.e they are not created correctly as when running the program directly as a "standalone"...

I thought that the array might be modified when passed to the @_ one in the subroutine. But if I print it line by line within the subroutine it looks OK.

I have tried to pass the array as a reference but it doesn't seem to work either (although I have to admit I am not an expert in doing this).

Could someone help me with this issue ?
 
you need to post some code here..

anyway the best way to pass arrays is something like this:

Code:
my @arr1 = ('1', '2', '3');
my @arr2 = ('4', '5', '6');
&func(\@arr1, \@arr2);

sub func
{
 my ($arr_ref1, $arr_ref2) = @_;
 my @sub_arr1 = @$arr_ref1; # Or use $arr_ref1->[0, 1...]
 my @sub_arr2 = @$arr_ref2; # Or use $arr_ref1->[0, 2...]
 ...
}
---
cheers!
san.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top