Could anyone help?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
michaelrezendes
Posts: 1
Joined: Thu Jul 25, 2024 5:31 am
Contact:

Could anyone help?

Post by michaelrezendes » Thu Jul 25, 2024 5:33 am

Good morning, I’m a college student struggling with Arrays in LiveCode. The code is as follows

global gDictionary

on mouseUp

Code: Select all

 answer “would you like to open a dictionary?” with “Yes” and “Cancel”

  if it is “Yes” then

        answer file “select a file”

        put URL “file: & theFilePath” into gDictionary

        split gDictionary by return and tab

        put gDictionary into fld “Keyword”

  else

  end if
end mouseUp

The objective is to display a word in the field “Keyword” while displaying its definition in the field “Definition.” The program needs to allow the user to add a word and its definition to the array as well as search through the array to find a specific word and it’s definition.

Help would be greatly appreciated, if you have more questions, please ask.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Could anyone help?

Post by Klaus » Thu Jul 25, 2024 8:54 am

Hi Michael,

welcome to the forum!

Code: Select all

...
answer “would you like to open a dictionary?” with “Yes” and “Cancel”
if it is “Yes” then
        answer file “select a file”

       ## Always check:
       if it = "cancel" then
          exit to top
       end if
        ## The path to the file is now in the variable IT:
        ## And use it OUTSIDE of the quotes!
        put url("file:" & IT) into gDictionary

        ## Your variable theFilePath is EMPTY!
        ## See above: use IT!
        ## put URL “file: & theFilePath” into gDictionary
        split gDictionary by return and tab
        put gDictionary into fld “Keyword”
## Not neccessary if there is no ELSE case!
## else
end if
...
Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Could anyone help?

Post by dunbarx » Thu Jul 25, 2024 1:53 pm

Hi.

You have other issues.

You can create an array variable using the "split" command, but you cannot display it in a field. Array variables simply do not work that way. Try this. On a new card make a button and a field. Put this in the button script:

Code: Select all

on mouseUp
   put "AA  BB" & return & "CC  DD" & return & "EE FF" into temp
   split temp by return and space
   put temp into fld 1
   breakpoint
end mouseUp
You will see nothing in the field, but in the Script Editor you can expand and see the variable "temp". Only "ordinary" variables can be viewed in a field. You would have to "combine" temp back into such a thing to view it in a field.

Craig

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Could anyone help?

Post by Klaus » Thu Jul 25, 2024 2:15 pm

Good catch, completely oversaw this. :oops:

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 115
Joined: Wed Apr 08, 2009 11:54 pm

Re: Could anyone help?

Post by dickey » Fri Jul 26, 2024 1:02 am

Hello Michael,

Another helpful resource getting started with arrays is https://lessons.livecode.com/m/4071/c/16765 , and whilst not exactly aligned with your requirements but hopefully helpful is some code below to have the user select a file, then process the lines of the file for use in displaying the content in for example a data grid.

Code: Select all

on mouseDown
   -- prompt the user to select a file
   answer file "Select a file to continue." 
   -- if the user selects a file
   if the result is not "Cancel" then
      put it into tFile
      open file tFile for read
      read from file tFile until EOF
      put it into tLines
      put 0 into i
      repeat for each line tLine in tLines
         add 1 to i
         -- skip the header row in the file
         if i >=2 then
            -- let's pretend your file is a .csv file with two columns "word" and "definition"
            -- split creates an array of items from a single line of the file that are separated by commas
            split tLine by comma
            -- you can then access values in the array using the index of the column within the array
            put tLine[1] into tWord
            put tLine[2] into tDefinition
            -- populate a data array for use with a data grid
            put tWord into tData[i]["Word"]
            put tDefinition into tData[i]["Definition"]
         end if
      end repeat
      
      set the dgData of group "DataGrid1" to tData
      ## select the first row of the data grid
      set the dgHilitedLines of group "DataGrid1" to 1
   else
      answer "No file was selected."
   end if
end mouseDown
Kind regards, Andrew

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Could anyone help?

Post by dunbarx » Fri Jul 26, 2024 1:48 am

Klaus.
Good catch, completely oversaw this
I can see why you appended the "embarrassed" emoji :D

And I must apologize right away. Why? Just ask me how my German is. :oops:

Craig :D

Post Reply