gemelli
Member
I know, I know ... to some of you, "recursion" is most likely a dirty word
That being said, I'd like some help with it just the same.
What I am trying to do is build an ASH function whose purpose is to examine an integer map, and return a string that specifies the keys in that map whose summed values come closest to a specified target value. The attached file is an abbreviated version of my function, stripped down to its base logic to illustrate the problems I'm having.
The purpose of this code is to try and test all possible combinations of the values in the statchange[] map, and to see which one of them comes closest to the given "howmuch" parameter. As it goes through, the function updates global variables `closest` (which stores the current "champion" value closest to howmuch), `closelist` (which stores the current list of map keys associated with the champion), and `closecount` (which stores the # of items in the champion list).
The basic flow is:
* Check the current summed value of the mapped integers defined by the keys in testlist
* If the summed value is closer to `howmuch` than the current closest value, crown a new champion
* If the summed value is equally close to `howmuch` than the current closest value, AND has fewer items in its list, crown a new champion
* Iterate through all of the keys in statchange[] to see if we can add a new one to testlist ... if so, recursively call the function with the appropriate appended term to testlist.
Based on how this would work in (for example) PHP, I would expect the function to recurse itself with the following values of testlist:
(null)
::1
::1::2
::1::2::3
::1::3
::2
::2::3
::3
However, the actual output is as follows:
There are a few things that seem to be going wrong here:
(1) The `foreach key in statchange {` code is running through once for a key of 0, even though no such key is defined in the statchange[] map
(2) Although findSum() is recursively called three times, the `Completed findSum` message only appears once
So there are clearly some aspects of ASH recursion that I don't understand
Can someone help point me in the right direction? (And if there's a simpler way to handle the "find a sum" problem described above, please let me know!)

What I am trying to do is build an ASH function whose purpose is to examine an integer map, and return a string that specifies the keys in that map whose summed values come closest to a specified target value. The attached file is an abbreviated version of my function, stripped down to its base logic to illustrate the problems I'm having.
The purpose of this code is to try and test all possible combinations of the values in the statchange[] map, and to see which one of them comes closest to the given "howmuch" parameter. As it goes through, the function updates global variables `closest` (which stores the current "champion" value closest to howmuch), `closelist` (which stores the current list of map keys associated with the champion), and `closecount` (which stores the # of items in the champion list).
The basic flow is:
* Check the current summed value of the mapped integers defined by the keys in testlist
* If the summed value is closer to `howmuch` than the current closest value, crown a new champion
* If the summed value is equally close to `howmuch` than the current closest value, AND has fewer items in its list, crown a new champion
* Iterate through all of the keys in statchange[] to see if we can add a new one to testlist ... if so, recursively call the function with the appropriate appended term to testlist.
Based on how this would work in (for example) PHP, I would expect the function to recurse itself with the following values of testlist:
(null)
::1
::1::2
::1::2::3
::1::3
::2
::2::3
::3
However, the actual output is as follows:
Code:
Testlist:
combined=0 from
Checking key 0
Checking key 1
Testlist: ::1
combined=2 from ::1
Checking key 0
Checking key 1
Checking key 2
Testlist: ::1::2
combined=-1 from ::1::2
Checking key 0
Checking key 1
Checking key 2
Checking key 3
Testlist: ::1::2::3
combined=-6 from ::1::2::3
Checking key 0
Checking key 1
Checking key 2
Checking key 3
Completed findSum for ::1::2::3
Checking key 3
Checking key 2
Checking key 3
Looking for a sum of: -8... we can do -6
Closest list: ::1::2::3
There are a few things that seem to be going wrong here:
(1) The `foreach key in statchange {` code is running through once for a key of 0, even though no such key is defined in the statchange[] map
(2) Although findSum() is recursively called three times, the `Completed findSum` message only appears once
So there are clearly some aspects of ASH recursion that I don't understand
