Page 1 of 1

How to Make Fields from Text File

Posted: Tue Aug 30, 2011 1:41 am
by jpottsx1
Hi, I have to make labels for about 65 items. Is there an easy way to read a text file and use each line as a label?

THX

Re: How to Make Fields from Text File

Posted: Tue Aug 30, 2011 2:14 am
by BarrySumpter
You can read a text file with tab delimited columns and return delimited rows into a scrolling datagrid.

http://lessons.runrev.com/spaces/lesson ... s/datagrid


MyTextFile.txt - <Tab> and <Return> are actual tab and return keys on the keyboard

Labels<Tab>ColumnHeader2<Tab>ColumnHeader3<Return>
FirstName:<Return>
SurName:<Return>
Email:<Return>
etc.

this should get you started in your research

Code: Select all

...
   if there is a file theFolderAndFileName then
      
      put url ("file:" & theFolderAndFileName) into theData
      put true into firstLineContainsColumnNames -- or false, depending
      set the dgText[firstLineContainsColumnNames] of group datagrid1 of card myCard to empty 
      set the dgText[firstLineContainsColumnNames] of group datagrid1 of card myCard to theData 
      
      -- Answer "File: " & theFolderAndFileName & " - Exists"
      
   else
      
      -- Answer "File: " & theFolderAndFileName & " - Does NOT Exist"
      
   end if

...
or you could loop thru theData and create Label fields as you go.
But thats a lot of LiveCode work setting all the properties up
for 65 fields on a single card.

May be too busy visually for the user.

Much tighter on a dataGrid.

hthnh - here to help - not hinder

Re: How to Make Fields from Text File

Posted: Tue Aug 30, 2011 1:27 pm
by Klaus
Hi Jeff,
jpottsx1 wrote:Hi, I have to make labels for about 65 items. Is there an easy way to read a text file and use each line as a label?
sure this is possible, but the scripting depends on how your "items" (= LiveCode objects?) are organized!
Waht label do you want to set? Of buttons? Just curious :)

Reading the file:
...
put url("file:" & "you path to file/here.txt") into tAllLabels
...
Now you have the text file in a variable that you can process, but see above, need a bit more info to continue.
Ideally the buttons (or whatever need to be LABELed) are numbered from 1 to X according to the lines in the text file!


Best

Klaus

Re: How to Make Fields from Text File

Posted: Tue Aug 30, 2011 1:50 pm
by dunbarx
Do you mean print actual labels? Klaus and Barry have spoken about an interpretation that you want to create 65 individual label fields, each containing a single line from your text? In either case, the solution is very simple. Write back.

Craig Newman

Re: How to Make Fields from Text File

Posted: Tue Aug 30, 2011 3:14 pm
by jpottsx1
I was trying to take a text file with one field delimited with a carriage return at the end of each record.
Example:
Feelings Of Guilt (cr)
Feeling Irritable And Intolerant Of Others (cr)
Lack Of Motivation And Little Interest In Things (cr)
Difficulty Making Decisions (cr)
Lack Of Enjoyment (cr)

I want to read the data, and then create labels (ie text blocks) on a card using one line of data for one label.

Re: How to Make Fields from Text File

Posted: Tue Aug 30, 2011 4:07 pm
by Klaus
Hi Jeff,
jpottsx1 wrote:I want to read the data, and then create labels (ie text blocks) on a card using one line of data for one label.
OK, reading the file is explained in my earlier post, now you can loop through the variable:

Code: Select all

...
repeat with i = 1 to the num of lines of tAllLabels
  ## do something with: line i of tAllLabels

  ## Example:
  put line 1 of tAllLabels into fld ("the label field" && i)
  ## Will work if you have 65 fields with the name "the label field 1" ..."the label field 65"
end repeat
...
But we still don't know what EXACTLY you want to to with the lines :D


Best

Klaus

Re: How to Make Fields from Text File

Posted: Tue Aug 30, 2011 4:13 pm
by jpottsx1
I just want to create a text label/Label Field on a card. ie the descriptive name of a field placed upon a card.

Re: How to Make Fields from Text File

Posted: Tue Aug 30, 2011 4:20 pm
by Klaus
Hi Jeff,

AHA! :D

OK, do this:

Code: Select all

...

## We need to make all newly created fields NOT editable!
set the locktext of the templatefield to true
repeat with i = 1 to the num of lines of tAllLabels
   put line i of tAllLabels into tLabel

   ## Create a new field 
   create field ("Label" && i)
   set the text of fld ("Label" && i) to tLabel

   ## now you ONLy need to take care of positioning your fields ;-)
   ## set the loc of fld ("Label" && i) to ???
end repeat
...
See also "templatefield" in the dictionary!

Best

Klaus