Text File Manipulation

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
wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Text File Manipulation

Post by wee wullie » Sun Sep 20, 2009 3:02 pm

Does anyone know of a way to get a certain amount of data from a text file, i.e. a text file that contains a list of recipe's in the format:

recipe title:

recipe notes:

recipe ingredients:

recipe directions:

**********

and how to make each recipe appear on a new card?

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Mon Sep 21, 2009 6:06 am

If the text file is relatively small (i.e. < 500K) the easiest way is to read it into a variable as a whole, and go through it line by line from there. Assuming you have setup a background groupo with all the necessary fields (title, notes, ingredients, directions), your import button script could look something like this.

Code: Select all

on mouseUp
  answer file "Readh which recipe file?"
  if the result is "Cancel" then exit mouseUp
  put "file:" & it into tURL
  put URL tURL into tData
  repeat for each line tLine in tData
    if tLine is "recipe title:" then
      create card  -- start a new recipe card
      put "title" into tCurrentField
    else if tLine is "recipe notes:" then
      put "notes" into tCurrentField
    else if tLine is "recipe ingredients:" then
      put "ingredients" into tCurrentField
    else if tLine is "recipe directions:" then
      put "directions" into tCurrentField
    else
      put tLine & return after field tCurrentField
    end if
  end repeat
end mouseUp
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

nearly

Post by wee wullie » Wed Sep 23, 2009 12:55 am

Hi Jan, thanks for your reply, i tried the code you posted and it filled two fields correctly: recipe ingredients is ok, recipe directions is ok but recipe Notes has no text and recipe title filled ok but it also has the text from the recipe notes field,
it nearly worked, it think it may just need tweaking slightly.

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Wed Sep 23, 2009 1:19 pm

I'm afraid you'll just have to go and debug it - I can't see a reason why it wouldn't work given your example. Perhaps the import file has an extra space before or after a colon?

Best of luck,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Sorted

Post by wee wullie » Fri Sep 25, 2009 12:52 pm

Thanks Jan,
I managed to get it sorted out in the end,

thanks for your help!

Post Reply