Putting fields into a 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
trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Putting fields into a text file

Post by trags3 » Fri Apr 25, 2014 2:19 am

I am trying to put data into a text file that I will subsequently be able to send with email.
The data is contained in label fields and text entry fields and are all in 2 adjacent columns on 1 card.
there are about 15 rows.
The math that adds and subtracts the various text entry fields works perfectly and the correct answer is the last field.
The code I have used is a series of put statements as follows:

put " " && fld "lf2" of card 3 && " " && fld "1numval1" of card 3 into line 16 of URL "file:sellnet.txt"
put " " && fld "lf3" of card 3 && " " && fld "1numval2" of card 3 into line 17 of URL "file:sellnet.txt"
put " " && fld "lf4" of card 3 && " " && fld "1numval3" of card 3 into line 18 of URL "file:sellnet.txt"
put " " && fld "lf5" of card 3 && " " && fld "1numval4" of card 3 into line 19 of URL "file:sellnet.txt"
put " " && fld "lf6" of card 3 && " " && fld "1numval5" of card 3 into line 20 of URL "file:sellnet.txt"
put " " && fld "lf7" of card 3 && " " && fld "1numval6" of card 3 into line 21 of URL "file:sellnet.txt"

etc.
All of the label fields (lfx) are correct and appear correctly in the sellnet.txt. file.
Most of the numbers (1numvalx) are placed correctly with minor adjustments in the spaces but several of the numbers are completely missing.
I know there is an easier way to do this but this seems like it should work.
Thoughts?

Thanks
Tom

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

Re: Putting fields into a text file

Post by dunbarx » Fri Apr 25, 2014 4:23 am

Hi.

You can certainly compact the long list of code lines by using a repeat loop with well managed indexes.

put " " && fld "lf2" of card 3 && " " && fld "1numval1" of card 3 into line 16 of URL "file:sellnet.txt"
put " " && fld "lf3" of card 3 && " " && fld "1numval2" of card 3 into line 17 of URL "file:sellnet.txt"

could be, (foe example, a representative line in a loop):

put " " && fld ("lf" & tfIndex) of card 3 && " " && fld ("1numval" & tNumValIndex) of card 3 into line 17 of URL "file:sellnet.txt"

But is this your question? In other words, does everything work, and you just want a more elegant handler?

Craig Newman

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Putting fields into a text file

Post by trags3 » Fri Apr 25, 2014 6:01 am

No, for some reason it doesn't work.. Some of the numbers don't show up in the text file at all.

Tom

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Putting fields into a text file

Post by magice » Fri Apr 25, 2014 2:15 pm

trags3 wrote:No, for some reason it doesn't work.. Some of the numbers don't show up in the text file at all.

Tom
Are the numbers that are not showing up values from the fields? If they are, I would strategically place "answer" lines within the script to narrow down where the values are being lost. You might find that it is something simple like a misnamed field.

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

Re: Putting fields into a text file

Post by dunbarx » Fri Apr 25, 2014 2:48 pm

Magice makes a point about checking your handler along its execution path. Another way that I use as well is to place values into arbitrary variables in the same way, sprinkled through the handler. This is simpler because you do not have to dismiss modal dialogs, you simply carefully watch these "extracted" values.

The choice of what to watch is your job. It could be the instant contents of those fields. You might assemble the accumulated data set into a variable, apart from your text file, and see how that builds. If there is a hiccup from one line to the next, you will likely find what is breaking the sequence.

Craig

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Putting fields into a text file

Post by trags3 » Fri Apr 25, 2014 3:43 pm

Here is what I have discovered. If I put the data on every other line the 3 numbers that have been calculated (and they happen to be the numbers that were missing) show up on the following line in column 1 of the text. How do I insure that a calculated numeric field contains only numbers and no "cr" , line feed, tab or other non visible characters? This is how the output text file looks.
Total Sales 560000

Cost of Goods 485000

Commissions
16800
Miscellaneous 0


Tom

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Putting fields into a text file

Post by magice » Fri Apr 25, 2014 4:45 pm

You could try referencing the first line of fld instead of its entire value, (Or second line if the cr is at the beginning). You could also put in an if statement that deletes a cr if the field ends with one (or begins as the case may be).

Another possibility, is to write the code that does the math in the form of a function. In that way, you control what is returned a little differently and any extra spaces and lines should be eliminated. This is why i like to work with variables instead of directly referencing field values.
Last edited by magice on Fri Apr 25, 2014 5:18 pm, edited 1 time in total.

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Putting fields into a text file

Post by trags3 » Fri Apr 25, 2014 5:15 pm

Solved it! The cr was in the lfx field. Just coincidentally in labels for the three fields that were calculated.

Tom

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Putting fields into a text file

Post by magice » Fri Apr 25, 2014 5:22 pm

trags3 wrote:Solved it! The cr was in the lfx field. Just coincidentally in labels for the three fields that were calculated.

Tom
Awesome, but now you have to think of the end user, and how they might accidentally make that same mistake. Consider having your script check the field value and confirm that the user has entered compatible information.

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

Re: Putting fields into a text file

Post by dunbarx » Fri Apr 25, 2014 5:27 pm

Well done.

This is an issue that mainly arises with a user entry field. It is deceptively easy to make such a field, but it requires care to validate whatever crap the user might decide to type into it. This is straightforward with such requirements as, say, numbers only, so that math operations do not break.

But it is a bit more insidious when an invisible character might be entered. Users are fond of hitting return when they finish typing. You would not notice this in a one line entry field. As Magice pointed out, a good way to eliminate a trailing cr is to extract, or even reload back into the field itself, only the first line.

You get the picture, anyway. The hard part is knowing what might cause problems down the road.

Craig

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Putting fields into a text file

Post by trags3 » Fri Apr 25, 2014 6:51 pm

Thanks guys. All of my numeric fields only allow numbers to be entered and on fields that may contain a "." I only allow one to be entered. I have 3 miscellaneous fields that the end user may enter the label so I will be careful to only allow entry of valid characters in these fields.
In this case I was the offending user and I have learned something.

Thanks for the help.

Tom

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Putting fields into a text file

Post by jacque » Sat Apr 26, 2014 7:57 pm

A quick, cheap way to disallow carriage returns in a field is to set the height of the field to a single line and then turn on autoTab. That forces a carriage return to tab to the next field rather than entering an actual return. Or you can just trap "returnInField".
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2948
Joined: Sun Sep 23, 2007 4:58 pm

Re: Putting fields into a text file

Post by SparkOut » Sat Apr 26, 2014 8:17 pm

You also could trap exitfield and closefield messages to strip unwanted cr and other characters, otherwise data pasted in rather than typed could contain the wrong things.

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

Re: Putting fields into a text file

Post by dunbarx » Sat Apr 26, 2014 9:32 pm

You could also....

Oh, never mind. The point here is that LC has so much flexibility it just isn't funny anymore.

Craig

Post Reply