Subtracting values from an array ASP

Posted on 2006-04-07
Last Modified: 2010-04-17
Say I have an array - dim myArray(3) - that holds four values: 4,2,0,0. The total values held in the array = 6. I want to subtract 5 from the the array so the new values would be to 0,1,0,0. What is the best way to do this.

I need to subtract delivered stock from previous orders in a data table. I'm guessing putting the values into an array first and then doing the maths before updating the values again would be the best way.

Question by:danzzz
    LVL 48

    Accepted Solution

    not too sure your logic behind, but you can try this...

          myArray = Array(4,2,0,0)
          subtract = 5
          for i = 0 to ubound(myArray)
                if subtract > myArray(i) then
                      subtract = subtract - myArray(i)
                      myArray(i) = 0
                      myArray(i) = myArray(i) - subtract
                      exit for
                end if
          response.write join(myArray,",")

    LVL 2

    Expert Comment

    It is probably easier to calculate:

    Previous Stock - Delivered Stock = New Value

    for each line of stock?

    Author Comment

    An invoice/packing list is generated from Indonesia each time a shipment of furniture is made. The container might fulfil some orders and parts of others. Adding to the confusion there are five different suppliers. As soon as the furniture is shipped the company lists the furniture as stock so the sales team can start selling it.  

    Previous Stock - Delivered Stock = New Value. This is what I'm doing but across different placed orders (database columns). I create a temporary column in the stock table for each stock order list and when that entire order has been fulfilled then I will delete the column. Depending on how quickly the suppliers manufacture the furniture they may send for example 20 chairs which might have to be matched to more than one order.

    thanks for reading this
    LVL 2

    Expert Comment

    Ah ok I follow now, the code above from ryancys should do what you want.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now