Page 1 of 1

Getting the sum of numbers with decimals

Posted: Sun Jun 30, 2013 3:25 am
by stoavio
Hi all,

It's hard for me to ask for help. I love to intuit things on my own but admittedly, I am a huge beginner and have reached an impasse after spending what seems like an inordinate amount of time trying to solve a seemingly simple problem.

I am writing a tiny program that reads the contents of transaction files and isolates the dollar amount to a field. Although it is exceedingly simple, an application like this is very useful in my department. Anyway....

The program is successfully reading the files and reporting the dollar amounts to a field named "Total" but NOW I'm trying to add up all of these amounts to get a total sum. I keep striking out. When using the sum function it keeps telling me the numbers I'm trying to add up aren't numbers (which I know is because of the decimal). I've tried removing the decimal and using the sum of fld "Total" but it still errors out. Maybe I can't use the sum function on a field or variable?

Code: Select all

on mouseUp
   local tFileName
   local tFileCount
   local tOutput
   local tTxntotal
   
   answer files "Choose your TRANS files:"
   put it into fld "Files"
   put number of lines in fld "Files" into tFileCount //get number of transactions selected
   
   put "Transactions Processed:"&&tFileCount into fld "Processed"
   
   repeat for number of lines in fld "Files" // loop to open and parse each file
      put line tFileCount in fld "Files" into tFileName //starting with last file first
      
      open file tFileName for read //open transaction
      read from file tFileName at 1 until EOF //read entire transaction
      put it into tOutput //store output
      put offset ("BATCHAMT", tOutput) into tTxnTotal //start search for check total
      read from file tFileName at tTxnTotal for 7 characters //isolate total
      put it into tTxnTotal
      replace space with empty in tTxntotal //strip whitespace
      
      put tTxnTotal after field "Total" //write the total of each transaction to field Total
      
      subtract 1 from tFileCount //decrement filename each time loop runs
   end repeat
   
end mouseUp
Can someone please help?

Re: Getting the sum of numbers with decimals

Posted: Sun Jun 30, 2013 3:58 am
by stoavio
I think I might've figured it out. I haven't tried it yet but I think my numbers need to be comma delimited to be processed by the sum function. I think I was wrong about the decimal messing it up. Going to try it now.

Re: Getting the sum of numbers with decimals

Posted: Sun Jun 30, 2013 4:08 am
by stoavio
Yep, that was it. I just had to take the contents of the field and put them into a variable, make them comma delimited and run the sum function. :)

Re: Getting the sum of numbers with decimals

Posted: Sun Jun 30, 2013 9:36 am
by Dixie
The 'add' command would also arrive at the total for you...

Code: Select all

on mouseUp
   put empty into total
   repeat with count = 1 to the number of lines of fld 1
      add line count of fld 1 to total
   end repeat
   put total
end mouseUp

Re: Getting the sum of numbers with decimals

Posted: Sun Jun 30, 2013 12:26 pm
by Klaus
Hi stoavio,

some hints:
1. you are not closing all the files that you "open..." in the repeat loop!
Not good ;-)

2. Maybe you would like the shorter URL syntax, which reduces files access to a one-liner :-)
And no need to CLOSE the file later, too!

Maybe something like this:

Code: Select all

on mouseUp
  local tFileName
  local tFileCount
  local tOutput
  local tTxntotal
  
  answer files "Choose your TRANS files:"
  put it into fld "Files"
  put number of lines of fld "Files" into tFileCount //get number of transactions selected
     
  put "Transactions Processed:"&&tFileCount into fld "Processed"
     
  ## Accessing FIELDS is much slower than putting their content into a variable first!
  put fld "Files" into tFileList
  
  ## Never access a field in a repeat loop, see above and below:
  put empty into tFieldTotal
  
  repeat with i = tFileCount down to 1
    put line tFileCount of tFileList into tFileName //starting with last file first
    
    ## The one liner to put a file into a variable!
    put url("file:" & tFileName) into tOutput
    
    put offset ("BATCHAMT", tOutput) into tTxnTotal //start search for check total
    
    ## This one might need a little adjustment ;-)
    put char tTxnTotal to (tTxnTotal + 7) of tOutput into tTxnTotal
    
    replace space with empty in tTxntotal //strip whitespace
    
    ## Write to variable first...
    put tTxnTotal after tFieldTotal
  end repeat
  
  ## ...then to field!
  put tFieldTotal after field "Total" //write the total of each transaction to field Total
end mouseUp

Best

Klaus

Re: Getting the sum of numbers with decimals

Posted: Sun Jun 30, 2013 2:59 pm
by dunbarx
This completely stumped me until I saw that the comma fixed it. I am frequently thrown by the fact that this forum is international, and that many posts are all about mobile.

Craig Newman

Re: Getting the sum of numbers with decimals

Posted: Wed Jul 17, 2013 7:50 pm
by stoavio
Klaus wrote:Hi stoavio,

some hints:
1. you are not closing all the files that you "open..." in the repeat loop!
Not good ;-)

2. Maybe you would like the shorter URL syntax, which reduces files access to a one-liner :-)
And no need to CLOSE the file later, too!

Maybe something like this:

Code: Select all

on mouseUp
  local tFileName
  local tFileCount
  local tOutput
  local tTxntotal
  
  answer files "Choose your TRANS files:"
  put it into fld "Files"
  put number of lines of fld "Files" into tFileCount //get number of transactions selected
     
  put "Transactions Processed:"&&tFileCount into fld "Processed"
     
  ## Accessing FIELDS is much slower than putting their content into a variable first!
  put fld "Files" into tFileList
  
  ## Never access a field in a repeat loop, see above and below:
  put empty into tFieldTotal
  
  repeat with i = tFileCount down to 1
    put line tFileCount of tFileList into tFileName //starting with last file first
    
    ## The one liner to put a file into a variable!
    put url("file:" & tFileName) into tOutput
    
    put offset ("BATCHAMT", tOutput) into tTxnTotal //start search for check total
    
    ## This one might need a little adjustment ;-)
    put char tTxnTotal to (tTxnTotal + 7) of tOutput into tTxnTotal
    
    replace space with empty in tTxntotal //strip whitespace
    
    ## Write to variable first...
    put tTxnTotal after tFieldTotal
  end repeat
  
  ## ...then to field!
  put tFieldTotal after field "Total" //write the total of each transaction to field Total
end mouseUp

Best

Klaus
Hey Klaus!

I just saw this. Thank you so much for your helpful response. I have implemented your suggestions. You have been a great resource.