Can someone explain to me how the opcodeFrom: method of private-incoming commands under Scratch-Networking works?
A quick TL;DR version:
Can someone explain how this code works: Scratch-Networking>ScratchServer>private-incoming commands>opcodeFrom:
I'm confused mostly at the end, where it says '(aString copyFrom: 1 to: i - 1) asLowercase'.
Offline
The method looks for the "op code" in a string.
The op code is a string of numbers at the beginning of aString followed by a space.
Here is a translated version of the code:
loop through each character in the string { if the current character is a space or we reached the end of aString { exit the loop and continue the method } otherwise { continue looking for the space } } return a substring of aString starting at the first character and ending on the character that the loop above exited at.
So here are some examples of putting strings through the method:
"1236 hello" becomes "1236"
"12 12 3" becomes "12"
"hello hello" becomes empty
" 123" (leading space) becomes empty
"126hello" becomes "126"
It's a pretty weird piece of code if you ask me... especially the choice of delimiter.
Offline
LS97 wrote:
The method looks for the "op code" in a string.
The op code is a string of numbers at the beginning of aString followed by a space.
Here is a translated version of the code:Code:
loop through each character in the string { if the current character is a space or we reached the end of aString { exit the loop and continue the method } otherwise { continue looking for the space } } return a substring of aString starting at the first character and ending on the character that the loop above exited at.So here are some examples of putting strings through the method:
"1236 hello" becomes "1236"
"12 12 3" becomes "12"
"hello hello" becomes empty
" 123" (leading space) becomes empty
"126hello" becomes "126"
It's a pretty weird piece of code if you ask me... especially the choice of delimiter.
I understood the loop, it was much like this Lua for loop:
for i = 1, string.len(str) do --code end
It was just the ending part that was REALLY puzzling me. Now I understand that it's done in an opposite format to Lua.
Offline
Well then next time it might help if you gave more information about what you actually understand and what you dont.
Offline
LS97 wrote:
Well then next time it might help if you gave more information about what you actually understand and what you dont.
Well, I did say:
"I'm confused mostly at the end, where it says '(aString copyFrom: 1 to: i - 1) asLowercase'."
I thought that would've been enough for people to understand that I didn't know what that part was, but the rest was OK. But it seems not. I'll try wording my sentences better next time.
Offline