How to use an array?

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

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

How to use an array?

Post by AlessioForconi » Fri Mar 25, 2022 8:47 am

Salutations,

I would like to replace the letters of the alphabet with numbers.

A = 1 b = 2 c = 3 ...

I thought I was using an array instead of a switch, 26 possibilities are too many, to find out what the letter is and extract its number.

However, I cannot implement it correctly. Could I have a simple example of how to do it?

Code: Select all

Function convertprimary mywords
    Put MyWords Into Pippo
    Repeat for Each Char Item in Pippo
       Add 1 to i
       Put Char I of Pippo Into letters
       ---> Here should be the array for replacement <---
       Answer letters
    End Repeat
End ConvertPrimary
thank you so much

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: How to use an array?

Post by AndyP » Fri Mar 25, 2022 11:00 am

No need to use arrays, try this in a button

Code: Select all

on mouseUp
  
  put "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26" into tNumbers
  put "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" into tLetters
  
  set the itemDel to comma
  
  repeat with c = 1 to the number of items in tNumbers
    put item c of tLetters into item c of tNumbers
  end repeat
  
  answer tNumbers
  
  --if you want to get rid of the commas
  replace "," with space in tNumbers
  
  answer tNumbers
  
end mouseUp
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to use an array?

Post by Klaus » Fri Mar 25, 2022 11:10 am

Buongiorno Alessio,

really not sure what you are looking for?
You want to have an array like:
a[1]
b[2]
etc...
and use this to replace the characters in a give text?
If yes, then do like this:

Code: Select all

Function convertprimary mywords
   ## Create the array:
   ## We will use repat for EACH so we need to manage a counter
   put 1 into tCounter
   put empty into tCharArray
   repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz"
      put tCounter into tCharArray[tChar]
      add 1 to tCounter
   end repeat
   
   ## Now replace each char with its number from the array
   ## We create a new variable which contains the replaced character
   put empty into myNewWords
   Repeat for Each Char tChar2 in mywords
      put tCharArray[tChar2] after myNewWords
   End Repeat
   return myNewWords
End ConvertPrimary
May need some refinement, but you get the basic idea.
converprimary.jpg
If this is not what you mean, please explain.


Best

Klaus

andresdt
Posts: 146
Joined: Fri Aug 16, 2019 7:51 pm

Re: How to use an array?

Post by andresdt » Fri Mar 25, 2022 1:56 pm

AlessioForconi wrote:
Fri Mar 25, 2022 8:47 am
Salutations,

I would like to replace the letters of the alphabet with numbers.

A = 1 b = 2 c = 3 ...

I thought I was using an array instead of a switch, 26 possibilities are too many, to find out what the letter is and extract its number.
Besides the solutions given here, I can think of two others.

1

Code: Select all

on mouseUp
    answer convertprimary("E")
end mouseUp

function convertprimary pLetter
   return itemOffSet(toUpper(pLetter),"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")
end convertprimary 

2

Code: Select all

on mouseUp
    answer convertprimary("E")
end mouseUp

local sLetterArray
function convertprimary pLetter
   if sLetterArray is not an array then
      local c = 0
      repeat for each item i in "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
         add 1 to c
         put c into sLetterArray[i]
      end repeat
   end if
   
   return sLetterArray[ pLetter ]
end convertprimary 
The first version besides being more compact is faster :D

---Edit---
What you have to do is call one of these two algorithms inside a loop.

Code: Select all

function LetterToNum pWord
   local tNexText
   repeat for each char tChar in pWord
      put convertprimary(tChar) after tNexText
   end repeat
   return tNexText
end LetterToNum
Last edited by andresdt on Fri Mar 25, 2022 2:02 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to use an array?

Post by dunbarx » Fri Mar 25, 2022 1:58 pm

Lots of ways to do what you want, but I also am wondering why you would want to.

With, say, Klaus' handler, you will end up with both one and two digit replacements for a body of text. You will need a routine to parse that numeric string. What will you do with spaces, actual numbers in your text or other characters besides alpha ones? How will you use the numeric string obtained?

At least think about this, with at least each derived char separated by a space:

Code: Select all

on mouseUp
   answer translateCharsToNumbers("my dog has fleas") 
end mouseUp

function translateCharsToNumbers var
   repeat for each char tChar in var
      put charTonum(tChar) - 96 & space after temp
   end repeat
   return temp
end translatecharsToNumbers
This is only valid for lower case letters, another issue perhaps for you to think about.

Craig

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to use an array?

Post by stam » Fri Mar 25, 2022 3:15 pm

Agree with all above - i was thinking that for this simple purpose a list (rather than array) would do - but then you don't even need that...
As Craig suggests you can just extrapolate from the ASCII code.

Personally i would use toUpper(<letter>) and then subtract 64 from the ascii code, so that A or a = 1, B or b = 2, etc. But basically what Craig says!

For the OP's benefit if wanting to learning more about arrays (above and beyond the documentation from LC), this may be helpful: http://livecodeblog.blogspot.com/2015/0 ... rrays.html

Stam

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to use an array?

Post by dunbarx » Fri Mar 25, 2022 3:26 pm

You can see that in my quick handler a dash ("-") is generated where a space was encountered in the source. Of course that can be detected and either exploited or ignored. Similarly, upper case source letters can be detected and similarly handled.

But it occurs to me again, that though the mechanics of building what you want are simple, I think that your purpose in all this may suggest a completely different approach.

So can you say what you are up to?

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to use an array?

Post by dunbarx » Fri Mar 25, 2022 3:28 pm

Stam.
i would use toUpper(<letter>) and then subtract 64 from the ascii code, so that A or a = 1, B or b = 2, etc.
A cute gadget. But I still wonder what the intent behind all this is. I bet the best solution will address that intent more than the coding mechanics.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to use an array?

Post by dunbarx » Fri Mar 25, 2022 3:41 pm

Stam's enhancement, and losing spaces and other fluff in the source text, is worth publishing here:

Code: Select all

on mouseUp
   answer translateCharsToNumbers("My dOg2 & ? HaS flEAs") 
end mouseUp

function translateCharsToNumbers var
   put "abcdefghijklmnopqrstuvwxyz" into pool
   repeat for each char tChar in var
      if tChar is in pool then put charTonum(toUpper(tChar)) - 64 & space after temp
   end repeat
   return temp
end translateCharsToNumbers


Craig

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to use an array?

Post by Klaus » Fri Mar 25, 2022 3:57 pm

Hi Craig,

numtochar() and chartonum() are deprecated since version 7 and may throw an error in later versions!
Get used to use bytetonum() and numtobyte() instead.

Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to use an array?

Post by dunbarx » Fri Mar 25, 2022 4:06 pm

Klaus.

I know, and you are correct. But I am going wait until my code breaks to change.

To everyone else, change now.

Craig

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to use an array?

Post by Klaus » Fri Mar 25, 2022 4:19 pm

Lazybone! :D

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to use an array?

Post by stam » Fri Mar 25, 2022 4:35 pm

actually numToByte() and byteToNum() is for binary data i think!

The correct versions of this would be numToNativeChar() and nativeCharToNum() ;)

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to use an array?

Post by Klaus » Fri Mar 25, 2022 4:59 pm

Ouch, you are correct, of course. :oops:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to use an array?

Post by dunbarx » Fri Mar 25, 2022 5:05 pm

Ouch?

I feel no pain.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”