﻿ Exiting For-loop in a search-subroutine
25 May, 2013, 18:09:45
 OmnomIRC You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

 Pages: [1]   Go Down
 Author Topic: Exiting For-loop in a search-subroutine -  (Read 391 times) 0 Members and 1 Guest are viewing this topic.
MGOS

Offline

Gender:
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297

Topic starter
Total Post Ratings: +74

 « on: 09 December, 2011, 14:27:53 » 0

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?

 123456789101112 Lbl SIf GFor (I,1,G)If {L1+I-1} = r1Goto FEndEndEnd0->ILbl FReturn I

 12345678910 Lbl SIf GFor (I,1,G)If {L1+I-1} = r1Return IEndEndEndReturn 0
 « Last Edit: 09 December, 2011, 15:24:50 by MGOS » Logged

Current
Projects:
aeTIos
You got stair'd!
LV12 Extreme Poster (Next: 5000)

Offline

Gender:
Date Registered: 15 September, 2010, 06:00:00
Location: Netherlands, Overijssel
Posts: 3121

Total Post Ratings: +120

 « Reply #1 on: 09 December, 2011, 15:39:07 » 0

So what you want to do is stopping the for() loop?
In that case, do this:
 123456 for(A,0,G)blah blahif condition to stop loop = 1G->Aendend
 Logged

If something above sounds rude, feel free to vote it down, it was not meant to be rude<<lolol
--Always stay relAXEd!--

Spoiler for Hidden:

[

Spoiler for Still Alive:
This was a triumph.
I'm making a note here: HUGE SUCCESS.
It's hard to overstate my satisfaction.

Aperture Science:
We do what we must because we can.
For the good of all of us
Except the ones who are dead.

But there's no sense crying over every mistake
You just keep on trying till you run out of cake
And the science gets done and you make a neat gun
For the people who are still alive.

I'm not even angry.
I'm being so sincere right now.
Even though you broke my heart and killed me.
And tore me to pieces.
And threw every piece into a fire.
As they burned it hurt because
I was so happy for you.

Now these points of data make a beautiful line
And we're out of beta we're releasing on time.
So I'm GLaD I got burned think of all the things we learned
For the people who are still alive.

I think I prefer to stay inside.
Maybe Black Mesa -
THAT WAS A JOKE. HA HA, FAT CHANCE.
Anyway, this cake is great:
It's so delicious and moist.

Look at me still talking when there's science to do.
When I look out there it makes me GLaD I'm not you.
I've experiments to run there is research to be done
On the people who are still alive

And believe me I am still alive.
I'm doing science and I'm still alive.
I feel FANTASTIC and I'm still alive.
While you're dying I'll be still alive.
And when you're dead I will be still alive.

Still alive
Still alive
MGOS

Offline

Gender:
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297

Topic starter
Total Post Ratings: +74

 « Reply #2 on: 09 December, 2011, 15:51:55 » 0

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: 04 September, 2012, 20:50:18 by MGOS » Logged

Current
Projects:
Builderboy
Physics Guru

Offline

Gender:
Date Registered: 20 April, 2009, 00:28:53
Location: Ravenholm
Posts: 5643

Total Post Ratings: +589

 « Reply #3 on: 09 December, 2011, 20:50:31 » 0

You could use InData() It does exactly what you describe, and is a built in Axe command ^^
 Logged

jacobly
LV4 Regular (Next: 200)

Offline

Date Registered: 09 October, 2011, 01:53:09
Posts: 199

Total Post Ratings: +149

 « Reply #4 on: 09 December, 2011, 21:07:28 » 0

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: 09 December, 2011, 21:26:55 by jacobly » Logged
AngelFish
This is my custom title
LV12 Extreme Poster (Next: 5000)

Offline

Gender:
Date Registered: 15 August, 2010, 09:18:54
Posts: 3191

Total Post Ratings: +221

 « Reply #5 on: 09 December, 2011, 21:26:08 » 0

Isn't this exactly what the CPIR instruction in z80 does?
 Logged

∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ
jacobly
LV4 Regular (Next: 200)

Offline

Date Registered: 09 October, 2011, 01:53:09
Posts: 199

Total Post Ratings: +149

 « Reply #6 on: 09 December, 2011, 21:42:29 » 0

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 ).
 Logged
AngelFish
This is my custom title
LV12 Extreme Poster (Next: 5000)

Offline

Gender:
Date Registered: 15 August, 2010, 09:18:54
Posts: 3191

Total Post Ratings: +221

 « Reply #7 on: 09 December, 2011, 21:47:15 » 0

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.
 Logged

∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ
squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)

Online

Gender:
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1837

Total Post Ratings: +478

 « Reply #8 on: 09 December, 2011, 22:33:53 » 0

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
 Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
Quigibo
The Executioner
LV11 Super Veteran (Next: 3000)

Offline

Gender:
Last Login: 21 May, 2013, 02:03:21
Date Registered: 22 January, 2010, 05:02:37
Location: Los Angeles
Posts: 2022

Total Post Ratings: +1019

 « Reply #9 on: 09 December, 2011, 22:54:06 » 0

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.
 Logged

___Axe_Parser___
Today the calculator, tomorrow the world!
MGOS

Offline

Gender:
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297

Topic starter
Total Post Ratings: +74

 « Reply #10 on: 09 December, 2011, 23:15:37 » 0

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: 04 September, 2012, 20:52:47 by MGOS » Logged