textfile into 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

Post Reply
propeller
Posts: 22
Joined: Sat Oct 13, 2018 12:22 pm

textfile into array

Post by propeller » Tue Nov 06, 2018 11:37 am

Hi there,

i am trying to load a textfile into an array and am stuck.

my textfile has 4 columns and looks like that (split by tabs):
name type xposition yposition - this goes on for a couple of lines

so i created a button which allows me to select the textfile and loads it into theData and displays it.

Code: Select all

on mouseUp
   answer file "A text file"
   if it <> "" then
      put it into theFilePath
      --don't forgett the brackets!
      put URL ("file:" & theFilePath) into theData
      answer theData
           
   else
      --no file was selected, or cancel was pressed
      beep
   end if
end mouseUp
my idea from here would be to put name of line 1 into theData[1][1], type of line 1 into theData[1][2] and so on but i have no clue on how to do it.

i would be glad if someone can push me into the right direction. have a nice day, you out there!

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

Re: textfile into array

Post by Klaus » Tue Nov 06, 2018 11:51 am

Servus Propeller,

do like this:

Code: Select all

...
## We use repeat for each, since this is lightning fast!
## However this requires that we manage our own counter:
   put 1 into tCounter
   set itemdel to tAB
   put empty into tArray
   repeat for each line i in tData
      repeat with k = 1 to 4
         put item k of i into tArray[tCounter][k]
      end repeat
      
      ## Increment counter
      add 1 to tCounter
   end repeat
   ## Fettich :-)
...
Best

Klaus

propeller
Posts: 22
Joined: Sat Oct 13, 2018 12:22 pm

Re: textfile into array

Post by propeller » Tue Nov 06, 2018 12:41 pm

Hallo Klaus,
it´s you again. by the way: can we talk german or for the sake of others reading my posts stay in english?
anyway - thank you for helping me again!

your code makes sense and i tried the following but there must be some empty space in my brain because i can not display specific items of the created array. i want to use items of the array as variables for the rest of my app, so this would be important.

please have another look on what i did and tell me where i am wrong.
on mouseUp
answer file "A text file"
if it <> "" then
put it into theFilePath
put URL ("file:" & theFilePath) into tData
put 1 into tCounter
set itemdel to tAB
put empty into tArray
repeat for each line i in tData
repeat with k = 1 to 4
put item k of i into tArray[tCounter][k]
end repeat

## Increment counter
add 1 to tCounter
end repeat

put tArray[1][1] into Field1
put tArray[1][2] into Field2
put tArray[1][3] into Field3
put tArray[1][4] into Field4

else
--no file was selected, or cancel was pressed
beep
end if
end mouseUp

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

Re: textfile into array

Post by Klaus » Tue Nov 06, 2018 1:53 pm

Hi Propeller,

since this is an english forum, we should talk english.

Code: Select all

...
put tArray[1][1] into Field1
put tArray[1][2] into Field2
put tArray[1][3] into Field3
put tArray[1][4] into Field4
---
Are Field1 etc. variables?
If yes, then you need to make them GLOBAL, to be able to use them in other handlers/scripts, too.

Code: Select all

...
global Field1,Field2,Field23,Field4
put tArray[1][1] into Field1
put tArray[1][2] into Field2
put tArray[1][3] into Field3
put tArray[1][4] into Field4
...
If these are meant to be Livecode textfield objects, then you have to tell LC so:

Code: Select all

...
put tArray[1][1] into fld "Field1"
put tArray[1][2] into fld "Field2"
put tArray[1][3] into fld "Field3"
put tArray[1][4] into fld "Field4"
---
If that is NOT what you mean, please explain.


Best

Klaus

propeller
Posts: 22
Joined: Sat Oct 13, 2018 12:22 pm

Re: textfile into array

Post by propeller » Tue Nov 06, 2018 2:47 pm

Thank you Klaus. Basically it was my mistake. i had a blank line in my textfile and so the first four items were blank. you made me happy again :)

i can now go on with my app but i am sure, we will get in contact again. have a nice day!

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

Re: textfile into array

Post by Klaus » Tue Nov 06, 2018 3:07 pm

Hi Propeller,

you can also catch this in your script:

Code: Select all

...
put empty into tArray
   repeat for each line i in tData
   if i = EMPTY then
     next repeat
  end if
...
Or even better:

Code: Select all

...
answer file "A text file"
if it <> "" then
  put it into theFilePath
  put URL ("file:" & theFilePath) into tData
  filter lines of tData without EMPTY
  ## :-)
...
Best

Klaus

propeller
Posts: 22
Joined: Sat Oct 13, 2018 12:22 pm

Re: textfile into array

Post by propeller » Tue Nov 06, 2018 3:40 pm

Ha, i am stuck again. after creating the array i try to make a global variable "Testfeld" and add some content in it. Then i have another button where i try to access the content int the "Testfeld" but it does not work. please lighten up my stupid brain again.

Code: Select all

on mouseUp
   answer file "A text file"
   if it <> "" then
      put it into theFilePath
      --don't forgett the brackets!
      put URL ("file:" & theFilePath) into tData
      //answer theData
      put 1 into tCounter
      set itemdel to tAB
      put empty into tArray
      
      repeat for each line i in tData
         repeat with k = 1 to 4
            put item k of i into tArray[tCounter][k]
            
         end repeat
         ## Increment counter
         add 1 to tCounter
      end repeat
      
      
      //populate Textfields 1-4 ----- THIS WORKS
      put tArray[1][1] into fld "Field1"
      put tArray[1][2] into fld "Field2"
      put tArray[1][3] into fld "Field3"
      put tArray[1][4] into fld "Field4"
      
      //create global Variable Testfeld and fill it with tArray[1][2] ----- THIS WORKS
      global Testfeld
      put tArray[1][2] into Testfeld
      answer Testfeld
      
   else
      --no file was selected, or cancel was pressed
      beep
   end if
end mouseUp

the other buttons script

Code: Select all

on mouseUp
   answer Testfeld
end mouseUp

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

Re: textfile into array

Post by Klaus » Tue Nov 06, 2018 4:25 pm

You have to declare global variables in every script you want to use it!
So this will do:

Code: Select all

on mouseUp
   global Testfeld
   answer Testfeld
end mouseUp
If you have more than one handler in a script you can declare globals once at the top of the script:

Code: Select all

global Testfeld

on mouseup
  answer Testfeld
end mouseup

on mouseenter
  set the tooltip of me to Testfeld
end mouseenter

## etc...
Best

Klaus

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

Re: textfile into array

Post by dunbarx » Tue Nov 06, 2018 4:31 pm

Hi.

When you ask for the variable "testfeld", LC will have no idea what you are referring to.

You could:

1-Make the variable global, in which case you must declare it in EACH handler:

Code: Select all

global testfeld
...other stuff...
2- You can save the variable in a custom property, which survives handlers and sessions. You could even save it in a field.

But the point is that a variable is discarded by LC once a handler ends. So it must be "saved" somehow.

Craig Newman

propeller
Posts: 22
Joined: Sat Oct 13, 2018 12:22 pm

Re: textfile into array

Post by propeller » Tue Nov 06, 2018 4:38 pm

Hej, you two fine guys. Thank you for your answers. I already thought that i might have to define this in another way. so what my plan is to create the array and then store it in a way that is can re-use its content from any place in my app. basically the textfile i generate the array from will contain some kind of preferences. i guess i will read myself through the topic of custom properties.

thank you again!

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

Re: textfile into array

Post by Klaus » Tue Nov 06, 2018 4:40 pm

Hi Propeller

send me an email and I will explain custom properties to you in german if you like:
klaus AT major-k.de


Best

Klaus

Post Reply