#49670 - 10/09/14 08:20 PM
adding a column
|
OL Expert
Registered: 05/12/03
Posts: 193
Loc: Los Angeles
|
Hi,
Believe it or not, I have been working on forms for 8 years and have NEVER had to add (sum) a column of numbers. What is the easiest way to do this?
I have a column of numbers that runs from line 23 to 33, and within columns 4-6.
Thank you.
|
Top
|
|
|
|
#49676 - 10/10/14 03:04 AM
Re: adding a column
[Re: FormMaster]
|
OL Guru
Registered: 08/29/08
Posts: 114
Loc: UK
|
Create a presstalk object and write this in it:
Define(&total, integer, 0)
¤t.line:= 23
Repeat()
&total:= &total + strtoint(@(¤t.line,4,6))
Until(¤t.line = 33)
Show(inttostr(&total))
|
Top
|
|
|
|
#49682 - 10/10/14 10:03 AM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 05/12/03
Posts: 193
Loc: Los Angeles
|
Thank you Dal. I will give this a try!
|
Top
|
|
|
|
#49683 - 10/10/14 10:14 AM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 05/12/03
Posts: 193
Loc: Los Angeles
|
Hmmm. I tried this, but I get a "string too long" error.
Edited by FormMaster (10/10/14 10:14 AM)
|
Top
|
|
|
|
#49684 - 10/10/14 10:24 AM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 04/19/10
Posts: 316
Loc: London, UK
|
"Loop too long" means you have a never-ending loop - the control variable is never incremented. Add ¤t.line := ¤t.line + 1 in the loop. Like this:
Define(&total, integer, 0)
¤t.line:= 23
Repeat()
&total:= &total + strtoint(@(¤t.line,4,6))
¤t.line := ¤t.line + 1
Until(¤t.line = 33)
Show(inttostr(&total))
Given that you have specified line range, I would probably use a For loop here...
Define(&total, integer, 0)
for(¤t.line, 23, 1, 33)
&total:= &total + strtoint(@(¤t.line,4,6))
endfor()
Show(inttostr(&total))
For loops automatically increment the control variable so remove the susceptibility to forget it.
Edited by jim3108 (10/10/14 10:32 AM) Edit Reason: Why do I need a reason?
|
Top
|
|
|
|
#49685 - 10/10/14 10:52 AM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 05/12/03
Posts: 193
Loc: Los Angeles
|
James,
This works correctly except it is only picking up the 3 digit numbers.
We're watching columns 4-6, but the data might be 1, 2, or 3 digits long - like this:
..2 ..5 .10 105 ..4 115
(the dots are empty space)
So right now, it's giving me a total of 220. it's only seeing the 3 digit numbers.
How do we make PP see all the numbers?
Thanks.
Edited by FormMaster (10/10/14 11:03 AM)
|
Top
|
|
|
|
#49686 - 10/10/14 11:18 AM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 04/19/10
Posts: 316
Loc: London, UK
|
OK. If the dots are empty space it should convert to the integer correctly. If they were dots it would need to be:
Define(&total, integer, 0)
for(¤t.line, 23, 1, 33)
&total:= &total + strtoint(strip('.',@(¤t.line,4,6)))
endfor()
Show(inttostr(&total))
Try trimming the selection before we convert:
Define(&total, integer, 0)
for(¤t.line, 23, 1, 33)
&total:= &total + strtoint(trim(@(¤t.line,4,6)))
endfor()
Show(inttostr(&total))
Is it at all possible that there is another character in the empty spaces?
Edited by jim3108 (10/10/14 11:23 AM) Edit Reason: Why do I need a reason?
|
Top
|
|
|
|
#49687 - 10/10/14 11:31 AM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 04/19/10
Posts: 316
Loc: London, UK
|
I have just tested this and it works. That is not to say that you aren't still having problems so let's look a little closer. Can you send me your data file?
Edited by jim3108 (10/10/14 11:35 AM) Edit Reason: Why do I need a reason?
|
Top
|
|
|
|
#49691 - 10/10/14 12:01 PM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 05/12/03
Posts: 193
Loc: Los Angeles
|
This is the strangest thing. I'm not getting any compile errors, but the box on the form is simply blank. Not even a zero.
Am I missing something? Do I need to do anything else beside create a PP object and place it on the form with the above code?
Yes. I will send the data file. Thank you.
Edited by FormMaster (10/10/14 12:05 PM)
|
Top
|
|
|
|
#49693 - 10/10/14 12:25 PM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 04/19/10
Posts: 316
Loc: London, UK
|
Ok I have found your problem. The 'spaces' before the numbers aren't spaces. They are what is called in Unicode: NO-BREAK SPACE Refer: http://en.wikipedia.org/wiki/Non-breaking_spaceI have sent you back a PP7 which calculates what you want. Just copy and paste the strip function when replicating this. Regards,
Edited by jim3108 (10/10/14 12:28 PM) Edit Reason: Why do I need a reason?
|
Top
|
|
|
|
#49694 - 10/10/14 12:38 PM
Re: adding a column
[Re: FormMaster]
|
OL Expert
Registered: 05/12/03
Posts: 193
Loc: Los Angeles
|
James, This works great. Thank you for responding so quickly.
|
Top
|
|
|
|
|
|