Announcement

Collapse
No announcement yet.

Easy way to add numbers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Easy way to add numbers

    Is there an easy way to add all items in a delimited string together, assuming they are all numbers?
    I have a bunch of figures in a list box, that I am trying to have added together, to show a total in a text object beneath. Right now I have it set so that on show, it gets all valuenames from a certain section in an ini file. Those valuenames' values are the figures being added. Never seems to work the same twice though...sometimes it works perfect, most of the time it doesnt. I am hoping there is an easy way to add the items in the list box.....
    Rob

  • #2
    Re: Easy way to add numbers

    When you say "added" are you asking for a way to create a mathematical sum or to concatenate the numbers into a string?

    For either it's easy, count the number of items in the source string using a STRING > COUNT DELIMITED STRINGS action and then just use a WHILE loop to step through every value in the string. For a sum add a line to your while loop which adds the current item value to a variable, i.e. %sumTotal%.

    Corey Milner
    Creative Director, Indigo Rose Software

    Comment


    • #3
      Re: Easy way to add numbers

      I am looking for a mathematical sum.
      I am still learning, and have yet to use a while loop. Could you go into a bit more detail?
      I know I would start off by getting all items from the listbox, but what are you saying to do from there?

      Comment


      • #4
        Re: Easy way to add numbers

        Sure, go to the section, "Example:Searching the desktop for text files" in the AMS help file, it is a working example of how to use a while loop in this very scenario. You can find this section by opening your help file and choosing "while loop" from the index tab...

        Corey Milner
        Creative Director, Indigo Rose Software

        Comment


        • #5
          Re: Easy way to add numbers

          I read through those sections, and it all seems pretty straitforward, basically the same as an if/end thing, except repeated over and over again until the required condition is found to be false. The part from your first message I didn't get was the "step through every value in the string" part.
          I know how to get the string, I know how to add two variables, but how would I, starting with a delimited string, use this while loop to step through each value, add them up, and end up with a sum in a variable I can set to a text object?

          Comment


          • #6
            Re: Easy way to add numbers

            Use the while loop to read in (or "step through" if you like) all the values one at a time. Try the example again, it's easy to figure out once you've done it...

            For example if my string has 10 items I set my while loop to 10, i.e. while %currentItem% < 10 {add %currentItemValue% to %totalSum%}

            Corey Milner
            Creative Director, Indigo Rose Software

            Comment


            • #7
              Re: Easy way to add numbers

              Ok, I tried that example. Still a little unsure, but this is what I came up with....

              <IR_ACTIONS_LIST>
              <Action name="Get Item Text">
              <Type>117</Type>
              <Function>0</Function>
              <DTIndentLevel>1</DTIndentLevel>
              <Enabled>1</Enabled>
              <ErrorHandling>
              <UserNotificationMode>2</UserNotificationMode>
              <CustomErrorMessage/>
              <OnErrorAction>0</OnErrorAction>
              <JumpToLabel/>
              </ErrorHandling>
              <TargetListBox>ListBox2</TargetListBox>
              <GetType>1</GetType>
              <Index/>
              <IndexDelimiter>;;</IndexDelimiter>
              <Variable>%figures2add%</Variable>
              <VarDelimiter>;;</VarDelimiter>
              </Action>
              <Action name="Count Delimited Strings">
              <Type>63</Type>
              <Function>0</Function>
              <DTIndentLevel>1</DTIndentLevel>
              <Enabled>1</Enabled>
              <ErrorHandling>
              <UserNotificationMode>2</UserNotificationMode>
              <CustomErrorMessage/>
              <OnErrorAction>0</OnErrorAction>
              <JumpToLabel/>
              </ErrorHandling>
              <Variable>%howmanyfigures%</Variable>
              <Source>%figures2add%</Source>
              <Delimiter>;;</Delimiter>
              </Action>
              <Action name="Set Value">
              <Type>6</Type>
              <Function>0</Function>
              <DTIndentLevel>1</DTIndentLevel>
              <Enabled>1</Enabled>
              <ErrorHandling>
              <UserNotificationMode>2</UserNotificationMode>
              <CustomErrorMessage/>
              <OnErrorAction>0</OnErrorAction>
              <JumpToLabel/>
              </ErrorHandling>
              <Variable>%currentfigure%</Variable>
              <Value>0</Value>
              <Evaluate>0</Evaluate>
              </Action>
              <Action name="WHILE">
              <Type>204</Type>
              <Function>1</Function>
              <DTIndentLevel>1</DTIndentLevel>
              <Enabled>1</Enabled>
              <ErrorHandling>
              <UserNotificationMode>2</UserNotificationMode>
              <CustomErrorMessage/>
              <OnErrorAction>0</OnErrorAction>
              <JumpToLabel/>
              </ErrorHandling>
              <Condition>%currentfigure% &amp;lt; %howmanyfigures%</Condition>
              </Action>
              <Action name="Get Delimited String">
              <Type>64</Type>
              <Function>0</Function>
              <DTIndentLevel>2</DTIndentLevel>
              <Enabled>1</Enabled>
              <ErrorHandling>
              <UserNotificationMode>2</UserNotificationMode>
              <CustomErrorMessage/>
              <OnErrorAction>0</OnErrorAction>
              <JumpToLabel/>
              </ErrorHandling>
              <Variable>%figuresamount%</Variable>
              <Source>%currentfigure%</Source>
              <Delimiter>;;</Delimiter>
              <ItemIndex>0</ItemIndex>
              </Action>
              <Action name="Set Value">
              <Type>6</Type>
              <Function>0</Function>
              <DTIndentLevel>2</DTIndentLevel>
              <Enabled>1</Enabled>
              <ErrorHandling>
              <UserNotificationMode>2</UserNotificationMode>
              <CustomErrorMessage/>
              <OnErrorAction>0</OnErrorAction>
              <JumpToLabel/>
              </ErrorHandling>
              <Variable>%total%</Variable>
              <Value>%total% + %figuresamount%</Value>
              <Evaluate>1</Evaluate>
              </Action>
              <Action name="END WHILE">
              <Type>205</Type>
              <Function>1</Function>
              <DTIndentLevel>1</DTIndentLevel>
              <Enabled>1</Enabled>
              <ErrorHandling>
              <UserNotificationMode>2</UserNotificationMode>
              <CustomErrorMessage/>
              <OnErrorAction>0</OnErrorAction>
              <JumpToLabel/>
              </ErrorHandling>
              </Action>
              </IR_ACTIONS_LIST>

              Is this anything close to what I would need to do?

              Comment


              • #8
                Re: Easy way to add numbers

                I'm not trying to be flippant but only you can answer that, i.e. does it do what you want it to do?

                It's important to note that applications built with AMS are so fast at performance time that even if your code isn't as elegant as it could be, your application will still be faster than you need.

                From my standpoint a tool is only useful when it does what you need. So if you have a need, i.e. to create a sum from a listbox, and you can figure out how to do it with a tool, i.e. AMS, then that's all that matters. So the only question is, did you manage to get a working solution to your needs? Sounds like it to me...

                The best solutions are the ones you invent because it not only gives your apps a personal touch unlike anyone else's but it also gives you the rush that you can only get from building something cool. That's why I try to point people in the right direction for resources and then let them solve things like this for themselves rather than dictate the code.

                Corey Milner
                Creative Director, Indigo Rose Software

                Comment


                • #9
                  Re: Easy way to add numbers

                  No, it did not work. It makes it through the loop, but when it sets my object text, it just says "%total%0"
                  This is basically the same problem I was having with my first method, where I was adding the items from the delimited list into a global list, and using the set position, current position, and item count actions for the global list to move through each one and add them.
                  I made a couple changes, where I found I had some variables mixed up, but still no luck. When it sets the text, I end up with one figure(I believe the last figure the loop got) , and the variable name, %total%, something like "50.00%total%".
                  I know people don't like doing your work for you, but I really need the help. I find the help files in AMS would be really usefull, if you already knew a lot about these things, but for the rest of us just getting started.......
                  Anyways, heres what I have, just assume that in a listbox there are a bunch of figures i.e: 50.00 42.02 etc.
                  I am getting these figures, which is the first line I have placed here. The last action, before the return, sets an object on my page using %total%. Can anyone see what I have done wrong here?
                  <IR_ACTIONS_LIST>
                  <Action name="Get Item Text">
                  <Type>117</Type>
                  <Function>0</Function>
                  <DTIndentLevel>1</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <TargetListBox>ListBox2</TargetListBox>
                  <GetType>1</GetType>
                  <Index/>
                  <IndexDelimiter>;;</IndexDelimiter>
                  <Variable>%figures2add%</Variable>
                  <VarDelimiter>;;</VarDelimiter>
                  </Action>
                  <Action name="Count Delimited Strings">
                  <Type>63</Type>
                  <Function>0</Function>
                  <DTIndentLevel>1</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <Variable>%howmanyfigures%</Variable>
                  <Source>%figures2add%</Source>
                  <Delimiter>;;</Delimiter>
                  </Action>
                  <Action name="Set Value">
                  <Type>6</Type>
                  <Function>0</Function>
                  <DTIndentLevel>1</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <Variable>%currentfigure%</Variable>
                  <Value>0</Value>
                  <Evaluate>0</Evaluate>
                  </Action>
                  <Action name="WHILE">
                  <Type>204</Type>
                  <Function>1</Function>
                  <DTIndentLevel>1</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <Condition>%currentfigure% &amp;lt; %howmanyfigures%</Condition>
                  </Action>
                  <Action name="Get Delimited String">
                  <Type>64</Type>
                  <Function>0</Function>
                  <DTIndentLevel>2</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <Variable>%figuresamount%</Variable>
                  <Source>%figures2add%</Source>
                  <Delimiter>;;</Delimiter>
                  <ItemIndex>%currentfigure%</ItemIndex>
                  </Action>
                  <Action name="Set Value">
                  <Type>6</Type>
                  <Function>0</Function>
                  <DTIndentLevel>2</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <Variable>%total%</Variable>
                  <Value>%total% + %figuresamount%</Value>
                  <Evaluate>1</Evaluate>
                  </Action>
                  <Action name="Set Value">
                  <Type>6</Type>
                  <Function>0</Function>
                  <DTIndentLevel>2</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <Variable>%currentfigure%</Variable>
                  <Value>%currentfigure% + 1</Value>
                  <Evaluate>1</Evaluate>
                  </Action>
                  <Action name="END WHILE">
                  <Type>205</Type>
                  <Function>1</Function>
                  <DTIndentLevel>1</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  </Action>
                  <Action name="Set Text">
                  <Type>75</Type>
                  <Function>0</Function>
                  <DTIndentLevel>1</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  <ObjectName>Text8</ObjectName>
                  <Text>%total%</Text>
                  </Action>
                  <Action name="RETURN">
                  <Type>207</Type>
                  <Function>1</Function>
                  <DTIndentLevel>1</DTIndentLevel>
                  <Enabled>1</Enabled>
                  <ErrorHandling>
                  <UserNotificationMode>2</UserNotificationMode>
                  <CustomErrorMessage/>
                  <OnErrorAction>0</OnErrorAction>
                  <JumpToLabel/>
                  </ErrorHandling>
                  </Action>
                  </IR_ACTIONS_LIST>

                  Thanks, Rob

                  Comment


                  • #10
                    Re: Easy way to add numbers

                    Don't bother with the global list. If you have a tab delimited string simply use a STRING > COUNT DELIMITED STRINGS action to count the items.

                    Use that number to set your while loop. Inside that loop reference the item which corresponds to the current while loop, add that item to the total, then increment your while loop.

                    That will work fine, I promise.

                    Corey Milner
                    Creative Director, Indigo Rose Software

                    Comment


                    • #11
                      Re: Easy way to add numbers

                      Ok, it was a stupid mistake. I realized now that I cant add %total% with %figureamount% if %total% isn't set to anything to begin with. before the loop did a %total%= "0" action, and seems to work great now.
                      Thank you Corey for your idea and your help. Not too many places you can get help at 5 in the morning [img]/ubbthreads/images/icons/smile.gif[/img]

                      Comment


                      • #12
                        Re: Easy way to add numbers

                        Anytime, but I didn't do anything, you solved it 100% yourself. [img]/ubbthreads/images/icons/smile.gif[/img] I respect your effort. Only good things can happen to people who try.

                        Corey Milner
                        Creative Director, Indigo Rose Software

                        Comment

                        Working...
                        X