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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.