Livecode repeat command - livecode

I have the bellow code and I want to complete the following.
I have a group with some fields.
I have 2 Variables. Variable 1 with names from fields and variable 2 with the values.
I want to set the text for the fields in the group to variable 2.
Line 1 from Var 1 = field name 1 -- Var 2 line 1 = value
Line 2 from Var 1 = field name 2 -- Var 2 Line 2 = value
repeat with x=1 to the number of flds of grp "flds_grp"
add 1 to counter
put the short name of fld x & cr after fldNames
end repeat
set the itemdel to comma
put 0 into myCounter
repeat for each line f in fldNames
repeat for each line t in tvalues
end repeat
end repeat

You have a variable counterbut it seems that you don't need this variable.
To set the text of field f to the value of variable t, use the following syntax:
set the text of field f to t

The right method have be answered from Sparkout in the Livecode forums.
repeat with x=1 to the number of flds of group "grp-A"
put line x of tValues into field x of group "grp-A"
end repeat

Related

How to add data to a graph from an API

I want to make a stock app, where the stock values of the past 7 days are shown in a graph. This is the code for extracting the data from the API:
on mouseUp
put url("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=YPMBUVQ8AJXX1HQP&datatype=csv") into myData
put myData into field "Data"
set the itemdel to ","
put 1 into x
repeat until x > 8
add 1 to x
put items 1 to 5 of line 5 of myData & return after gData
end repeat
set the graphData of widget "graph" to gData
end mouseUp
The first item will be the x axis, and the rest will all be on the y axis. But when I run this code, it only puts a single line into the graphData of the graph, and nothing shows up on the graph except for the 2 axis. What am I doing wrong here?
I tried the following variation that appears to work. One issue is your data contains a volume amount at the end of each line that's super high compared to the trading values, so I delete that value from each of the lines used for the chart.
on mouseUp
put url("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=YPMBUVQ8AJXX1HQP&datatype=csv") into temp
delete line 1 of temp -- REMOVE THE COLUMN LABLES
delete line 8 to -1 of temp -- LIMIT DATA TO 7 DAYS
repeat for each line theLine in temp
delete last item of theLine -- IGNORE VOLUME (NUMBER IS TOO LARGE COMPARED TO TRADING DATA)
put theLine & return after myData
end repeat
set the graphData of widget "graph" to myData
end mouseUp

How can I separate all the characters of a word into separate fields?

I have a random word generation script that works perfectly. I also have a few single character-width fields, exactly the same amount of them as the longest possible word that can be generated. Below each field I have a line that would resemble an underscore. Each field has a unique id, numbered from one like so: "letter1", "letter2".... and so on. Likewise, each line has the id of "line1", "line2", and so on.
Now that I've set the stage, here is what I want to happen. On the opening of the card, I want all of the letter fields to hide as I will have them show individually outside this subroutine. I then want all of the lines to hide, and then only the amount of lines that are required for the random word should show again. (The same number as length(randomword) from left to right.) Now, the random word should be looped through and the first character should be put into the field "letter1", and the second into "letter2", and so on. The word will change each time the card is opened, so it is important that this is not bruteforced (which unfortunately, myself having two weeks experience with LiveCode, is the only thing I know concretely how to do).
Could someone with more experience do me a solid and provide the code I would need to do this, and the location I should put the code in? (the card? the letter fields? I really don't know at this point).
I am happy to provide further description if I have not been articulate about my problem.
Try this (my LiveCode is a bit rusty):
on openCard
//Put your word generation script here
put 0 into amtOfLines
put 0 into amtOfLetters
repeat until amtOfLines is 6 //Replace 6 with the amount of lines
add 1 to amtOfLines
put "line" & amtOfLines into x
set the visible of graphic x to false
end repeat
repeat until amtOfLetters is 6 //Replace 6 with the amount of fields
add 1 to amtOfLetters
put "letter" & amtOfLetters into x
set the visible of field x to false
end repeat
put 0 into amtOfLines
put 0 into amtOfLetters
repeat until amtOfLines is length(randomword)
add 1 to amtOfLines
put "line" & amtOfLines into x
set the visible of graphic x to true
end repeat
repeat until amtOfLetters is length(randomword)
add 1 to amtOfLetters
put "letter" & amtOfLetters into x
set the visible of field x to true
end repeat
put 0 into amtOfChars
repeat until amtOfChars is length(randomword)
add 1 to amtOfChars
put "letter" & amtOfChars into x
set the text of field x to char amtOfChars of randomword
end repeat
end openCard
This would go in the card script.
EDIT: Fixed some errors.
This would go into the card script:
on setup pRandomWord
put 6 into tNumObjs -- or however many fields there are
put length(pRandomWord) into tLen
repeat with x = 1 to tNumObjs
hide fld ("letter"&x)
if x <= tLen then
put char x of pRandomWord into fld ("letter"&x)
show grc ("line"&x)
else
hide grc ("line" & x)
end if
end repeat
end setup
When you want to use it, put the random word into a variable and call the setup handler like this:
setup randomWord
This does everything in a single repeat loop. The random word is passed to the setup handler in the parameter "pRandomWord".

How to count the sum of numbers in a field in Livecode

I have an Scrolling Field and it's congaing number and word separated by space.
I want to find the sum of number (eg: 5 USA &CR 5 Uk)
on mouseUp
if the field "CC" is not empty then//here "CC" is an Scrolling field and it's containing the content
put 0 into aa
put fld "CC" into myData
split myData by CR
put the number of lines of (the keys of myData) into myArraylength
repeat with i = 1 to myArraylength
put 0 into zo
put myData[i] into y
split y by space
put y[1] into searchStr
if y[1]is not a number then
put 0 into var1
else
put searchStr into vari
put vari &comma after ss1
end if
end repeat
answer ss1
put sum(ss1) into aa1
answer aa1
put ss1 into second1
split second1 by comma
else
answer "List is Empty"
end if
end mouseUp
Assuming the text in your scrolling field is formatted consistently as in your example:
5 USA
5 UK
4 EUR
etc.
You can do something like this:
put fld "myScrollingFld" into tList
put 0 into tTotal
repeat for each line tLineContents in tList
put word 1 of tList into tNum
if tNum is a number then add tNum to tTotal
end if
on mouseUp
repeat for each word thisWord in fld "Myfield"
if thisWord is a number then add thisWord to tSum
end repeat
answer tSum
end mouseUp

How to count occurance in livecode?

I need to count the number of occurance of word in the array from the field "MytextField". The array consist of more than 9000 lines and the text in field "MytextField" consist of more than 10000 lines. I am using the following code and it works fine but need so much time.
put the number of lines of (the keys of myArray) into myArrayL
repeat with i = 0 to myArrayL
put myArray[i] into k
split k by colon
put k[1] into searchStr
put k[2] into replaceStr
repeat for each line iword in Tex
if iword contains searchStr then
add 1 to tmp
put tmp & " " & searchStr & cr into sam
end if
--delete word iword of Tex
if iword contains replaceStr then
add 1 to tmp1
put tmp1 & " " & replaceStr & cr into sam1
end if
--delete word iword of Tex
end repeat
put sam after slu
put 0 into tmp
put "" into sam
put sam1 after slu1
put 0 into tmp1
put "" into sam1
end repeat
answer slu1
answer slu
Is it any way to decrease the time consumption?
How to change this code with more speed
Try using offsets, which should be faster than examining every word:
put fld "MytextField" into tText
set the itemdel to colon
repeat for each element e in myArrayL
put item 1 of e into searchStr
put 0 into tSkip
repeat
get wordOffset(searchStr,tText,tSkip)
if it = 0 then exit repeat
add it to tSkip
add 1 to tCountArray[searchStr]
end repeat
end repeat
That counts the search words and puts the counts into an array. You can alter it a bit and run it again if you need to count the replacement words too. Don't count the search words and replacement words in the same repeat loop, you'll get incorrect results. I don't know if this will be much faster than your original script because the repeat loop has to run twice if you want both search and replacement word counts.
After the arrays are made, you can use the combine command to quickly show the counts.
Try the repeat for each element construct instead. It is generally faster in situations like this than repeat with i = x to y forms.
repeat for each element thisElement in myArray
# your loop logic here
end repeat

Find first column with empty cell in every third row using VBA

As the title says, I need to find the first column with an empty cell in every third row in my sheet.
It looks like this:
-----------
---------
-------
------
I need to write data from another sheet into each (third) row. That part of the code is ok, I checked it. But, for some reason, this code doesn't work:
For t = 5 To 500 Step 3
u = 0
For s = 5 To 500
If IsEmpty(Cells(t, s)) And s > g And u = 0 Then
g = s
u = 1
Exit For
End If
Next s
Next t
Its not clear what your problem is, but this will find the first blank cell in every third row and change it to a value you assign it.
Sub firstBlankEveryThirdRow()
For t = 5 To ActiveSheet.UsedRange.Rows.Count Step 3
Range("1:1").Offset(t - 1).Cells.SpecialCells(xlCellTypeBlanks)(1).Value = "Insert Your Value Here"
Next t
End Sub

Resources