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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays as parameters 1

Status
Not open for further replies.

xjc539w

IS-IT--Management
Aug 5, 2003
11
0
0
US
Any best pratices for accomplishing the following:

a.plx is the main program, that wants a module to work on a pair of arrays and an associated hash.

( @a1, @a2, %a3 ) = b::work( @a1, @a2, %a3 );

over in b.pm, I have a sub work. I need to process the arrays and hash then return back to the main.
 
If you just want to modify some variables "in place" so to speak, then use references.
Code:
my @a = qw/refs are good/;
my @b = qw/sometimes they make bad calls/;

work_it_out_baby(\@a, \@b);  # pass a reference (with the backslash)

print 'I think ', join(' ', @a), "\n";
print 'Yoda says: ', join(' ', @b), "\n";

sub work_it_out_baby {
    my($ary_ref1, $ary_ref2) = @_;  # you are expecting refs here
    $ary_ref1->[2] = 'great';
    @$ary_ref2 =  reverse @$ary_ref2;
    return;
}
Since your subroutine will be accessing the original data directly, instead of copies of the data, you don't have to worry about reassigning the variables like you used to.

Hope this helps...

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top