Recognizing character "-"

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
oldummy
Posts: 97
Joined: Mon Jun 13, 2016 3:21 pm

Recognizing character "-"

Post by oldummy » Sun Feb 28, 2021 2:49 am

I'm playing with a morse code stack just to renew some coding skills,few as they may be.
I have an entry field where I put in some text, in this case "test".
I have another button that(using cps) that translates the letters to "-" or "." and puts it into fld "bla". This works ok.
I have audioclips for dit and dah imported into the stack.

I have a button that should play these dits or dahs audioclips as they are needed, but I seem to be having some trouble with playing them. I am wondering if the character "-" has some sort of strange reaction in LC.

Here is the script of the button that is supposed to do this:
on mouseUp
put fld "bla" into thebox -- the fld with the text "test" in this case
repeat for each char thechar in thebox
if thechar is "-" then put 1 after x--play ac "dah.wav" --I switched playing the ac to putting a digit instead

wait 4 ticks
if thechar is "." then put 2 after x--play ac "dit.wav"--I switched playing the ac to putting a digit instead

wait 4 ticks
end repeat
put x -- I keep getting 2222 rather than 1 2 2 2 2 1
end mouseUp

--These are the dits and dahs in fld "bla" _ . ... _
--Is this something with the "-" character?

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Recognizing character "-"

Post by bogs » Sun Feb 28, 2021 8:08 am

oldummy wrote:
Sun Feb 28, 2021 2:49 am
--Is this something with the "-" character?
No, but you might try putting the hyphen in instead of the underscore ;)

This " _ . ... _" does not == " - . ... -"

Or, I suppose you could change your code to check for the underscore :D
Image

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Recognizing character "-"

Post by AxWald » Sun Feb 28, 2021 12:30 pm

Hi,

just checked out of curiosity. This works:

Code: Select all

on mouseUp
   get fld "input_fld"                                                         --  where there's my input
   
   repeat for each char C in it
      if C = "-" then
         put 1 after myVar                                                          --  add a value for -
      else if C = "." then
         put 2 after myVar                                                          --  add a value for .
      else
         put "?" after myVar                             --  in case we have something unwanted there ...
      end if
   end repeat
   put myVar                                                            --  send result to msg for review
   
   repeat for each char C in myVar
      if C = "?" then next repeat                                                          --  skip those
      if C = "1" then
         play ac "1.wav"                                                   --  play imported audio clip 1
      else if C = "2" then
         play ac "2.wav"                                                   --  play imported audio clip 2
      end if
      wait 500 millisecs with messages                                   --  some room between sounds ...
   end repeat
end mouseUp
"Imported as control" are 2 short audio clips. They play a fine morsing.

I did it in 2 loops, so I can show a visual representation what's to be played after. And I check for unwanted input - always possible that spaces, CRs or such sneak into our field ...
And I "wait" only after a sound has played, long enough to give a suitable "pause".

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

oldummy
Posts: 97
Joined: Mon Jun 13, 2016 3:21 pm

Re: Recognizing character "-"

Post by oldummy » Sun Feb 28, 2021 12:43 pm

Hence my userid
Thanks to all. I was thinking I may have posted a little too soon,

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Recognizing character "-"

Post by bogs » Sun Feb 28, 2021 1:02 pm

Heh, well, I dunno about too soon, but if you want some purely graphical input, I'd stick with the underscores instead of the dashes for visual representation :D
Image

stam
Posts: 3072
Joined: Sun Jun 04, 2006 9:39 pm

Re: Recognizing character "-"

Post by stam » Sun Feb 28, 2021 5:34 pm

May be nothing at all to do with your issue, but be aware that especially on Windows there is a difference between a minus sign and a hyphen (the latter being a bit more emphasised and wider), and often a “space” & minus & “space” will autocorrect to a minus sign to a hyphen, so code that relies on the former will fail...

Post Reply