How to limit data entry to only lowercase letters in LiveCode? - livecode

I am making a two-player hangman game, and I have set a limit of 10 characters which is working fine:
on keyDown
if the length of me = 10 then
beep
else
pass keyDown
end if
end keyDown
However, when I try to limit the character input to only lowercase characters, nothing seems to happen. I can still type any character I want.
on keyDown inkey
if inkey is in "abcdefghijklmnopqrstuvwxyz" then
pass keyDown
else
beep
end if
end keyDown
How do I fix this? Thankyou.

Don't pass the keyDown message. Instead, put the value of the parameter after the field.
on keyDown theKey
if len(the text of me) >= 10 then
beep
else
put toLower(theKey) after me
end if
end keyDown
The problem occurs, because LiveCode considers lower case and upper case characters as equal:
put ("a"="A")
returns true. Another way to solve this is to set the caseSensitive to true. If you add this line
set the caseSensitive to true
to the beginning of your script, it will do exactly what you want.

Related

Been struggling since few days ,How this 'continue' statement leaves 'a' and prints the rest of letter

my_string='Sammy'
for letter in my_string:
if letter =='a':
continue
print(letter)
Ok i understood continue statement goes back top of closest enclosing loop, but how leaves 'm'
and prints the rest of letters... . I know i definitely missing some part could not catch it for days ?
If you want to understand how "continue" works, you have to understand how "break" works first.
In a loop, if your code sees break, the most close loop closes. What does it mean? Let's take a look at your code but change the "continue" with "break"
my_string='Sammy'
for letter in my_string:
if letter =='a':
break
print(letter)
What do you think will happen? In normal circumstances, that for loop will print all the characters in your string While doing that, at letter a your code encounter a "break". Break will close your loop instantly. That means, your code will only be printing "S" letter since you're checking the letter first, and then printing it. Lets follow the code,
1) Create a variable named my_string and assign "Sammy" to it
2) Start a loop in letters of my_string variable
2-1) Is the letter "a"? No It's a "S"
2-2) Print the S
2-3) Go next letter
2-4) Is the letter "a"? Yes it is.
2-4-1) break
2-4-2) break the most close loop
2-4
2
3) Is there any more thing to do? No,
4) Close the program
Now lets talk about the "continue". Continue does similar thing too but instead of closing the loop complately, it just goes next state. Lets run your code this time.
my_string='Sammy'
for letter in my_string:
if letter =='a':
continue
print(letter)
1) Create a variable named my_string and assign "Sammy" to it
2) Start a loop in letters of my_string variable
2-1) Is the letter "a"? No It's "S"
2-2) Print the "S"
2-3) Go next letter
2-4) Is the letter "a"? Yes it is.
2-4-1) continue
2-4-2) go next state (ignore the rest of the loop)
2-5) Is the letter "a"? No It's "m"
2-6) Print the "m"
2-7 Is the letter "a"? No It's "m" again
2-8) Print the "m"
2-9) Is the letter "a"? No It's "y"
2-10) Print the "y"
2-11) Is there any more letters? No there isn't. Close the loop
2-
3) Is there anymore thing to do? No there isn't
4) Close the program
Continue send you to the next state, not the beggining of the current state or something.
If you write something like this,
a = 5
while a<10:
continue
a=a+1
You will enter a infinite loop because everytime you see the continue, your code will go the next state. But it's a while loop and while loops doesn't do anything about its variables. In next state, the a will be 5 again and again.

AHK Remove every second char from variable

I need a code on AHK
I have a variable look like this:
CYOMYACHOAYJGUGRYYQNYB
I need to get this:
YMAHAJURYNB
I meen, i need every second char from a variable. Thank in advance
Var := "CYOMYACHOAYJGUGRYYQNYB"
Loop, Parse, Var ; retrieves each character from the variable, one at a time
{
If (Mod(A_Index, 2) = 0) ; If A_Index is even (the remainder after division by 2 is 0)
NewVar .= A_LoopField ; add the retrieved character to the new variable
}
MsgBox %NewVar%
This works for me. I am using bit wise to determine if the index of the array of letters, given to me by StrSplit(TestString), is even or odd as I loop through them. I used this forum post for the bitwise logic. Then I concatenate if the line is even. So if index&1=0 will be true when the number is even, thus giving me every other letter to concatenate into NewString with this line NewString=%NewString%%letter%. Feel free to uncomment out the message box lines by removing the ; to better see how the loop parses the array.
TestString := "ABCD"
word_array := StrSplit(TestString)
NewString:=""
For index, letter in word_array{
if index&1=0
{
;MsgBox, %letter% added
NewString=%NewString%%letter%
;Msgbox, %NewString%
}
}
MsgBox, %NewString%
As you don't specify any language, I'll answer in pseudocode:
set counter to 1
set result to empty string
for every char in string:
if counter is even:
append char to result
increment counter by 1
user3419297 beat me to it, but mine is even easier:
InputBox, x, What Variable?, Enter Variable:
loop, % StrLen(x)
If mod(A_Index,2)=0
y.=substr(x,A_Index,1)
msgbox %y%
Clipboard := y
You input the variable in a dialog, and the result is shown, and put in clipboard. Hth,
EDIT: I like the bitwise logic from Zack Tarr! Substitute for the "if" above:
If A_Index&1=0
The rest is the same.

How can I check if a char is in the alphabet?

I've been researching the keyDown operator and I can't seem to find anywhere if its possible to write a program with on keyDown key without having to deal with non alphabetic chars. Hopefully someone with a bit more experience could let me know how to do this!
To check that the parameter of the keyDown message contains a letter, check the ASCII value:
if (charToNum(theKey) >= 97 and charToNum(theKey) <= 122) and (charToNum(theKey) >= 65 and charToNum(theKey) <= 90) then
or use the matchText() function:
if matchText(theKey,"[a-zA-Z]") then

How to fetch content in livecode

I want to catch content between two $ symbols.
Eg: - This is $ only$ an example $so$ respond $quickly$.
Here I want to store text between dollars (only, so and quickly) into an array.
In using this code for catching. But it catches (only, an example, so, respond, quickly). I need "only", "so" and "quickly".
replace "\$" with "\XXdollarXX" in field "MytextField"
put the text of field "MytextField" into ss
repeat with i = 0 to the number of chars in ss
if char i of ss contains "$" then
repeat with x = i+1 to the number of chars in ss
if char x of ss contains "$" then
--answer x
put x into Lpos
put char i to Lpos of ss into jar
answer jar
put Lpos into i
end if
end repeat
end if
end repeat
If you examine your code while stepping through, you will see this has to work that way. The "offset" function, though, can be used in a loop, and contains a parameter "chars to skip". By continuously updating that parameter, and considering that only every other instance is pertinent, you can collect the text between the pairs of "$" as:
between instance 1 and 2
between instance 3 and 4
etc.
Make a button and a field. Put some text into the field sprinkled with "$" for testing. In the button script:
on mouseUp
put 0 into charsToSkip
repeat until charsToskip > the length of fld 1
if the optionKey is down then exit to top --YOU WILL NEED THIS RIGHT NOW...
get offset("$",fld 1,charsToSkip)
add it to charsToSkip
put charsToSkip & return after accum
end repeat
end mouseUp
I leave it to you to add the ability to exit this loop properly. Step through the handler and you will see the value in accum building in the way I mentioned.
Craig Newman

How to get or set a character at specified index in fortran string?

program polynomialCalc
character(Len=100):: polynomialString="fds"
Do i=1, Len(Trim(polynomialString))
Print*, i, polynomialString(i:1)
END Do
END program polynomialCalc
I have no idea why code showed above returns only first character "f". I would be appreciate for explanation and good solution, how to handle this.
The other question is how can I set a character at specified index?
To get a specific character, use the same index for the start and end of your substring range. For example:
polynomialString(i:i)
Will be the single character at position i.
To set a character at a specific location you can do
polynomialString(pos:pos) = 'c'
To set characters from a position to a position, you can do
polynomialString(posFrom:posTo) = "12"
You can play around with them and see how they work.

Resources