its off topic but how about this style:
programming since 1984
B4J
B4X:
Sub Process_Globals
'Private fx As JFX
'Private xui As XUI
Public MainForm As Form
Type TPlayer(ID As Int,Name As String,Score As Int,Selected As Boolean,Debug As Boolean)
Public PlayerList As List
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
NewPlayer("John")
NewPlayer("Susan")
DebugPlayers
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub NewPlayer(Name As String) As TPlayer
If Name = "" Then Name = "?"
If PlayerList.IsInitialized = False Then PlayerList.Initialize
Dim PlayerX As TPlayer
PlayerX.Initialize
PlayerX.ID = PlayerList.Size + 1
PlayerX.Name = Name
PlayerX.Debug = True
PlayerList.Add(PlayerX) '<- Global List
Return PlayerX
End Sub
Sub DebugPlayers
For Each X As TPlayer In PlayerList
If X.Debug = True Then
Log($"Debug Entry: ${X.ID} ${X.Name}"$)
End If
Next
End Sub
Using your example (assuming you had other fields in you type)
B4X:
dim Entry as int
dim player as TPlayer
PlayerList.SortType("ZipCode", false)
for Entry = 0 to PlayerList.Size-1
player = PlayerList.get(Entry)
if player.Name = John then
' Do something like move this entry
end if
next
Now doing it with a For Each
B4X:
PlayerList.SortType("ZipCode", false)
for each player as TPlayer in PlayerList
' Do something with player
' Not knowing the Index of where we are at I cannot move the entry or do some other things
next
So let's do it
B4X:
dim Entry as int = 0
PlayerList.SortType("ZipCode", false)
for each player as TPlayer in PlayerList
' Do something with player
Entry = Entry + 1
next
Next doing it with for each
Now I know under the covers the compiler is generating an Index to reference the table entries.
I am just saying why not let us share the index
B4X:
PlayerList.SortType("ZipCode", false)
for each player as TPlayer in PlayerList using Entry
' Now we are looping the players and using or sharing "Entry" with the compiler
next
If you want even more syntax (for readability only)
B4X:
PlayerList.SortType("ZipCode", false)
for each player as TPlayer in PlayerList using Entry as index
' Now we are looping the players and using or sharing "Entry" with the compiler
next
This was just a thought.
I am always trying to make my code more readable (just an old fart that cannot change their ways LOL)
That might be true for array-like containers, but is probably untrue for iterating through linked list or tree or file directory structures, or anything dynamic.
I am guessing that it's because, if an index is available, then using For I = 0 to NumItems - 1 is not particularly onerous. Although I suspect also maybe because there would be substantial support time consumed in repeatedly explaining why the index is not consistently available.
I am quite happy that Anywhere Software is focusing their resources on areas that have the greatest leverage ie Our_Results / Their_Effort. Like, I'd love to have INCR and DECR operators (particularly since they are natively supported by the JVM) but... am we better off with those, or with some other useful feature that we can't already easily do another way?
Not sure what you mean. In this case TPlayer is a Type that could have 30+ fields in it.
I am sure that I am not explaining myself properly.
I either do my loops with the for i = 0 to ... if I need to be able to do a Get from the List otherwise I do a for each if I don't.
But sometimes I find after I have written a for each loop that I actually need to know where I am in the table (for whatever reason it may be)
This is strictly something I thought would be a easy add on to for each
You said: "That might be true for array-like containers, but is probably untrue for iterating through linked list or tree or file directory structures, or anything dynamic."
I didn't realize that for each was smart enough to follow a link list array (have only used standard arrays and lists)
if you use "for i = 0..." you cannot move the entry from 1 position to another because the whole loop will not work as expected.
ps. if you set an integer before the loop and increase it on each step you will have the position where you are so you can do the same thing as you do in your loop.
note that for each.. will also go through the loop from 0 to list.size-1 as it goes via for i = 0 to...
from what i know for each is faster then for i ...
the question is really what you are doing in your loop and what you mean with move the item. you can do anything in a for each loop like you do in a for i loop.
Because of the same mistakes I regulatory made while I try to improve my Java skills, forget ? the ; character at the end of the Java statement? Yes, man for the young programmers (in their twenties ) from the seventies the years are really going to count now. ?