Author Topic: Exiting For-loop in a search-subroutine  (Read 3933 times)

0 Members and 1 Guest are viewing this topic.

Offline MGOS

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +95/-0
    • View Profile
Exiting For-loop in a search-subroutine
« on: December 09, 2011, 07:27:53 am »
I have some question for a routine I was about to write:
The function should search the array L1 with the dimension G for the value r1. It should return the index of the first found value, or 0 if not found.

I had these two ideas how to do this, but Is the second legal axe code?
If so, which one is faster?
Do I have to put a space between Return and I?

Code: [Select]
Lbl S
If G
For (I,1,G)
If {L1+I-1} = r1
Goto F
End
End
End
0->I
Lbl F
Return I

Code: [Select]
Lbl S
If G
For (I,1,G)
If {L1+I-1} = r1
Return I
End
End
End
Return 0
« Last Edit: December 09, 2011, 08:24:50 am by MGOS »

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Exiting For-loop in a search-subroutine
« Reply #1 on: December 09, 2011, 08:39:07 am »
So what you want to do is stopping the for() loop?
In that case, do this:
Code: [Select]
for(A,0,G)
blah blah
if condition to stop loop = 1
G->A
end
end
I'm not a nerd but I pretend:

Offline MGOS

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +95/-0
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #2 on: December 09, 2011, 08:51:55 am »
Not only. I want to stop the for loop if the condition is true. But the function should return the value the index variable of the loop had when it was stopped.
So lets say the Array has the dimension of 5 and is {1,2,0,9,7} and r1 is 9. Then my function goes through the loop, stops at I = 4 and returns 4.
If I looked for let's say 5, the function would return 0, becouse none of the elements in the array had the value 5.

Both of the functions I posted SHOULD do this, but I don't know which one is better / legal axe code.

I know that array indizes usally start with 0, but starting with 1 has some advantages in my case, so I use that.
« Last Edit: September 04, 2012, 02:50:18 pm by MGOS »

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #3 on: December 09, 2011, 01:50:31 pm »
You could use InData() :D It does exactly what you describe, and is a built in Axe command ^^

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #4 on: December 09, 2011, 02:07:28 pm »
Well, InData() works with zero terminated data, and his routine works with G bytes of data and embedded zeros.

First of all, {L1+I-1} can be optimized to {I-1+L1}. Other than that, the second one should work fine. Also, the space should be optional.

Edit:
Spoiler For Crazy Optimizations:
Lbl S
Return!If G .returns G if G=0
For(I,1,G)
!If {I-1+L1}-r1
Return I
End
End
.as long as I is not modified inside the for loop, and G >= 1 (true), ans = -1
Return+1
« Last Edit: December 09, 2011, 02:26:55 pm by jacobly »

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #5 on: December 09, 2011, 02:26:08 pm »
Isn't this exactly what the CPIR instruction in z80 does?
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #6 on: December 09, 2011, 02:42:29 pm »
Almost. CPIR returns an address, whereas this routine returns an index. Also, this routine is much easier to extend to arrays of words or objects (but I'm not saying that access to CPIR through an axe command wouldn't be cool ;)).

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #7 on: December 09, 2011, 02:47:15 pm »
An address is pretty easily converted into an index by simply subtracting the base address of the array. As for the flexibility of the routine, yes. However, a single CPU instruction with a single subtraction afterwards is going to be far faster and smaller than anything Axe compiles to.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #8 on: December 09, 2011, 03:33:53 pm »
Side note that there is almost no such thing as illegal Axe code. If it works, then it's legal. Some of Axe's side effects created through 'hackish' code can be used to your advantage.

Goto-ing out of a For() loop is totally acceptable, but I like the method you used with Returns better

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #9 on: December 09, 2011, 03:54:06 pm »
The only problem with using hackish effects is that anything not specified explicitly by the commands list or documentation (like the inside/outside "ans" value in a for loop) might change from version to version, especially as more optimizations are made.  So use at your own risk, you just have a small possibility of losing the portability. :)
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline MGOS

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +95/-0
    • View Profile
Re: Exiting For-loop in a search-subroutine
« Reply #10 on: December 09, 2011, 04:15:37 pm »
Huge thanks to all of you, guys. I think I'm gonna use the version with the returns. That with searching the array for values is just a simplyfied version of what I want to do, actually the condition is a lot more than just that.
« Last Edit: September 04, 2012, 02:52:47 pm by MGOS »