Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

Looping through Array Logic

$
0
0
I have an interesting problem, that I have been attempting to solve for weeks.
I have 3 arrays
1 array is for tracking numbers
1 array is for products
1 array is for product Quantities

I have to code a solution that will "fill" the boxes. I have been able to code all of the scenarios except for when there are more boxes than products.
So I have the following:

Code:

Dim PackagesArray(4), OffersArray(2), QTYArray(2)

OffersArray(0) = "Line Item 1"
OffersArray(1) = "Line Item 2"
OffersArray(2) = "Line Item 3"


QTYArray(0) = 3
QTYArray(1) = 7
QTYArray(2) = 3

PackagesArray(0) = "Tracking 1"
PackagesArray(1) = "Tracking 2"
PackagesArray(2) = "Tracking 3"
PackagesArray(3) = "Tracking 4"
PackagesArray(4) = "Tracking 5"

What I am attempting to output is

[Tracking 1]
Line Item 1 - 2

[Tracking 2]
Line Item 1 - 1
Line Item 2 - 1

[Tracking 3]
Line Item 2 - 2

[Tracking 4]
Line Item 2 - 2

[Tracking 5]
Line Item 2 - 2
Line Item 3 - 3

Basically divide up the items and "Put" them into a box and put the remaining items into the last box.

Below is my test code and I can't seem to figure out how to accomplish this. Any pointers would be helpful.

Code:

LineItems = Ubound(OffersArray)
For a = 0 To Ubound(OffersArray)
        TotalQty = TotalQty+QTYArray(a)
Next

BoxTotal = 0
PackNum = 0 'Package Number
wscript.echo "Total Items: " & TotalQty
ItemsPerBox = fix(TotalQty/Boxes)
wscript.echo "ItemsPerBox: " & ItemsPerBox
wscript.echo Ubound(PackagesArray)

For i = 0 To Ubound(OffersArray)
        LeftOver = QTYArray(i) Mod ItemsPerBox
        LineBoxes = fix(QTYArray(i)/ItemsPerBox)
               
        QTY = QTYArray(i)
       
        Do Until QTY = 0
               
                'wscript.echo "SpaceLeft: " & SpaceLeft
                'Check to see if the last box and the last item
                If PackNum = Ubound(PackagesArray) Then
                       
                End If
                If QTY < ItemsPerBox Then
                        If BoxTotal = 0 Then
                                wscript.echo "  "
                                wscript.echo "[=="& PackagesArray(PackNum) & "==]"
                        End If
                        wscript.echo "(" & QTY &")"& OffersArray(i) & "------"
                        BoxTotal = Boxtotal + QTY
                        QTY=QTY-QTY
                ElseIf QTY > ItemsPerBox AND BoxTotal > 0 Then
                        SpaceLeft = ItemsPerBox - BoxTotal
                        If SpaceLeft > QTY Then
                                wscript.echo "(" & QTY & ")" & OffersArray(i) & "!!!!!!"
                                BoxTotal = BoxTotal + QTY
                                Qty= 0
                        Else
                                wscript.echo "(" & SpaceLeft & ")" & OffersArray(i) & "^^^^^^"
                                BoxTotal = 0
                                PackNum = PackNum+1
                                Qty=Qty - SpaceLeft
                                'QTY=0
                        End If
                Else                       
                        If PackNum = Ubound(PackagesArray) Then
                                wscript.echo "  "
                                wscript.echo "[==" & PackagesArray(PackNum) & "==]"                               
                                'BoxTotal = BoxTotal
                                wscript.echo "(" & QTY &")" & OffersArray(i) & ">>>>>>"
                                QTY=QTY-QTY
                        Else
                                wscript.echo "  "
                                wscript.echo "[==" & PackagesArray(PackNum) & "==]"
                                wscript.echo "(" & ItemsPerBox & ")" & OffersArray(i) & "*******"
                               
                                BoxTotal = 0
                                QTY=QTY - ItemsPerBox
                                If Packnum=Ubound(PackagesArray) Then                               
                                        PackNum = PackNum
                                Else
                                        PackNum = Packnum+1
                                End If
                        End If
                       
                End If
        Loop       
Next


Viewing all articles
Browse latest Browse all 688

Latest Images

Trending Articles



Latest Images