Iterating a string
BlitzMax Forums/BlitzMax Programming/Iterating a string
| ||
Hi, how would one make a loop that goes through the chars of a string one at a time? Something like this? for local s$ = eachin(str) if(s="a") then print "oregano" endif next Only this wont work... |
| ||
Oh how could I forget! ...the bracket indexer []For Local s% = 0 Until str.length If(str[s..(s+1)]="a") Then Print "oregano" EndIf Next |
| ||
orFor Local i = 0 Until s.length If s[i]="a"[0] Then Print "oregano" EndIf next its a bit faster than using slices. Last edited 2012 |
| ||
Oooooh Jesse! "a"[0] ?? XD If s[i]=Asc("a") |
| ||
Oooooh col! "a"[0] is similar Asc("a") ;) cheers Last edited 2012 |
| ||
Asc("a") optimises down to a literal 97 in the assembly output. "a"[0] doesn't, for whatever reason, and actually extracts the value each time (not that this is likely to make any noticeable difference). Last edited 2012 |
| ||
@Yasha: Funny to see that Monkey does the oposite. |
| ||
Which output are you checking? If you mean assembly post-C++, my guess would be that the C++ compiler catches the constant array access, 'cause they can do that easily, but -flto or equivalent isn't on and it's therefore not "aware" of Asc, which is presumably implemented as a library function for interoperability's sake (is it? I have no idea). If you mean human-readable intermediate output in whatever language, the Monkey compiler probably doesn't bother with inlining at all (that is, it wouldn't if I was designing it), because it's rather pointless when all of the major target-compilers will take care of that step anyway. |
| ||
@col I knew that. I was just throwing a curve :-). |
| ||
Const ASC_A:Byte = 97 For Local i:Int = 0 Until s.Length If s[i] = ASC_A Then Print "oregano" Next Enjoy ;) Last edited 2012 Last edited 2012 |