How to Make Fields from Text File

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
jpottsx1
Posts: 46
Joined: Thu Jun 04, 2009 12:46 am
Contact:

How to Make Fields from Text File

Post by jpottsx1 » Tue Aug 30, 2011 1:41 am

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
Jeff G potts

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: How to Make Fields from Text File

Post by BarrySumpter » Tue Aug 30, 2011 2:14 am

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
Last edited by BarrySumpter on Tue Aug 30, 2011 10:15 pm, edited 1 time in total.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

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

Re: How to Make Fields from Text File

Post by Klaus » Tue Aug 30, 2011 1:27 pm

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

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

Re: How to Make Fields from Text File

Post by dunbarx » Tue Aug 30, 2011 1:50 pm

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

jpottsx1
Posts: 46
Joined: Thu Jun 04, 2009 12:46 am
Contact:

Re: How to Make Fields from Text File

Post by jpottsx1 » Tue Aug 30, 2011 3:14 pm

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.
Jeff G potts

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

Re: How to Make Fields from Text File

Post by Klaus » Tue Aug 30, 2011 4:07 pm

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

jpottsx1
Posts: 46
Joined: Thu Jun 04, 2009 12:46 am
Contact:

Re: How to Make Fields from Text File

Post by jpottsx1 » Tue Aug 30, 2011 4:13 pm

I just want to create a text label/Label Field on a card. ie the descriptive name of a field placed upon a card.
Jeff G potts

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

Re: How to Make Fields from Text File

Post by Klaus » Tue Aug 30, 2011 4:20 pm

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

Post Reply