http://qs1969.pair.com?node_id=671135

why_bird has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Me again.. this time I'm having a problem with changing values in a subroutine. When a certain event occurs (my data returns '0' or 'inf') I need to print some things to a file (that's fine) and then clear the values of some other variables, to start again.

The array of data is determined by the main body of the program and is normally reset at the end of the while loop, and the counter counts from (say) 1 - 10 during the loop and again needs to be reset at the end of the loop.

My variables are locally scoped, so if I try changing them directly in the subroutine, nothing happens: (as expected)

sub subNext { ... ## print some stuff my $print_=$_[0] print FILE $print_; ... $count=0; @data=(); next; } while ( some condition ){ ... ## some stuff (subNext breaks to the next instance of the loop) ... my @data; ## process to fill @data with data my @count; ## count increments in various places during the loop during the loop if ($some_var == 0){ &subNext($print); } ... ## rest of code ... }

So then I tried passing by reference:

sub subNext { ... ## print some stuff $print_=$_[0] print FILE $print_; ... print "count is: $$_[2] , reference is: $_[2] \n"; print "data is: @$_[1] , reference is: $_[1] \n"; $_[2]=0; $_[1]=(); next; } while ( some condition ){ ... ## some stuff (subNext breaks to the next instance of the loop) ... my @data; ## process to fill @data with data my @count; ## count increments in various places during the loop if ($some_var == 0){ &subNext($print, \@data, \$count); } ... ## rest of code ... }

That prints
count is: , reference is: SCALAR(0x5147a0)
data is: , reference is: ARRAY(0x5146b0)
And netiher the counter nor the data array are reset. Can anyone help me understand what I'm doing wrong?

Thanks!