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

Passing in a previous value into subroutine...

Status
Not open for further replies.

StormMedic85

Programmer
Sep 7, 2007
20
US
I'd like to write a subroutine that performs an average of a variable over a specific layer of the atmosphere. In order to do the averaging, I need to pass in the values I wish to average (that part's easy), the current altitude (also easy), but then I also need the previous altitude since I am doing a backwards difference average.

In other words, I need to determine the thickness of the layer (current level - previous level). I am uncertain of an efficient way to pass in the previous altitude of a data set.

One way I was thinking, though inefficient, would be to return both the average and current altitude in the subroutine, then push the current altitude into an array and pass it back into the subroutine for the other values. Does anybody have any other suggestions? I'm thinking that perhaps I could do something with a for loop...like $alt[$i]-$alt[$i-1], but I'm not sure.

Thanks!
 
You can create your subroutine inside a block with variables defined at the level outside the subroutine. This allows you to have variables equivalent (for this purpose) as a static variable in C.

Simple example code:
Code:
[olive][b]while[/b][/olive] [red]([/red] <DATA> [red])[/red] [red]{[/red]
  [maroon]add_it[/maroon][red]([/red][blue]$_[/blue][red])[/red][red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [maroon]add_it[/maroon][red]([/red][fuchsia]0[/fuchsia][red])[/red][red];[/red]


[red]{[/red]
  [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$sum[/blue] = [fuchsia]0[/fuchsia][red];[/red]
  [url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]add_it[/maroon]
  [red]{[/red]
    [blue]$sum[/blue] += [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url][red];[/red]
  [red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]1[/teal]
[teal]2[/teal]
[teal]3[/teal]
[teal]4[/teal]

The scalar $sum doesn't exist just within the scope of the add_it() function. In your application, you'd store something like $previous_layer there, and you'd save your current layer to that scalar at the end of any successful calls.
 
Sounds like it shoud be no problem. Do you already have some code written?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I have something like the following but its obviously not correct. It also seems circular. The initial value for $prevalt should be 0, then should be rewritten with the previous altitude that was passed into the subroutine each time it's called. I think the way I have it now $prevalt is static and not changing. I'm probably making this harder than it has to be.

$layer = &thickness($alt[$i]); #This part is within a for loop that already works fine.

$prevalt = $alt;

sub thickness {

my ($alt, $layer, $prevalt);
$alt = $_[0];
$layer = $alt - $prevalt;
return ($layer,$alt);
}

 
pass a reference to the array, and the index? or simply have the sub process the array by walking through it ...

I guess what I'm saying is, without knowing how your data is stored, and/or could be stored, there's a plethora of ways to suggest improvement ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
this discussion is better continued in the newer thread:


StormMedic85,

There was no need to post a new thread to continue this discussion. In the future try and keep the same question going in one thread. Only post a new thread if there is a new question unrealted to the current topic.

Regards,
Kevin

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top