Page 1 of 1

Parameters and Global Variables

Posted: Fri Sep 15, 2017 1:45 pm
by smith8867
So I have code within a button:

Code: Select all

global fileData, newData, 

on mouseUp
   put empty into fileData
   put empty into newData
   readFile @fileData, @newData
end mouseUp

on readFile @fileData, @newData
   //Ask the user for the file in question and use the location
   //of the file to put the contents into the variable fileData
   answer file "Please choose the file to read..."
   if the result is not "Cancel" then
      put it into fileLocation
      put url("file:" & fileLocation) into newData
   else if the result = "Cancel" then 
      exit to top
   end if
   
   //Split and organise the filedata into a 2-D array
   local loop, tempData
   repeat with loop = 0 to the number of lines of newData
      //Take each line and split it into each individual component
      //and store it in its correct position in the 2D array.
      put line loop of newData into tempData
      split tempData by comma
      put tempData[1] into fileData[loop][1]
      put tempData[2] into fileData[loop][2]
      put tempData[3] into fileData[loop][3]
      put tempData[4] into fileData[loop][4]
   end repeat
   //Check the read was successful
   //If so, enable the button to display the information
   //If not, display message and quit
   if fileData is not empty then
      answer info "File read sucessfull!"
      enable button "processbtn"
   else 
      answer error "Something went wrong.."
      exit to top
   end if
And then I have a process button, that will put the 2D array data into a datagrid, however, when I declare the global variable and try and output, say fileData[1][1], it displays blank. However, on the first button when I put the answer fileData[1][1] it displays the information?
I feel like its something pretty stupid that I'm overlooking, but any help would be appreciated.
Thanks

Re: Parameters and Global Variables

Posted: Fri Sep 15, 2017 2:40 pm
by bogs
Off the top of my head with not enough coffee, I am wondering if you put the global in the second buttons script at the top, like it is in the first buttons script?

Not sure how you test for info in the second buttons output, did you use an answer box? if not, try putting the output to an answer box. If it shows up, check the code putting it into the dg.

All I got at the moment.

*edit - after stepping through the code (and putting the global line in the 2nd button), I can tell you that both globals have info right up till you hit the 'end readFile', then both globals blank to no information, so by the time you get to your 'processbtn' code, there is nothing to put anywhere.
Image
Image
Image

This may be a good case for using a custom property instead of a global variable, since you could read it into the custProp and not muck about with the global stuff, you would have to handle clearing it after your info is in the grid or before you put more info in it.

Re: Parameters and Global Variables

Posted: Fri Sep 15, 2017 5:23 pm
by jacque
Since these are global params, remove the parameters from the receiving handler entirely. You don't need to pass globals at all, they are always available to the script. And except for filling them with data, you don't need to send them in the calling handler either.

Also, on the rare occasions when you do need to use an @ sign , the calling handler shouldn't include them, only the parameters in the receiving handler. This won't apply here because you don't need to use params at all in this case.

Re: Parameters and Global Variables

Posted: Fri Sep 15, 2017 6:08 pm
by smith8867
Thanks guys! That really helps!