Output only the object 1 at a time

So I want to get the value of all items in the table I created, It prints correctly but my problem is it repeat again. Any help?

The script:

local shop = {
  --Format: [Item-ID] = {ItemName, Price, Weight}
  ["0001"] = {"Apple", 100, 1},
  ["0002"] = {"Pine-Apple", 250, 3},
  ["0003"] = {"Golden Tomato", 500, 10},
  ["0004"] = {"Potion +20", 300, 2},
  ["0005"] = {"Potion +100", 300, 2},
  ["1000"] = {"Plus Bag Capacity", 1000, 0}
}

for index1, itemID in pairs(shop) do
  for index2, item in pairs(itemID) do
    local itemName = itemID[1]
    local itemPrice = itemID[2]
    local itemWeight = itemID[3]
    print("itemID: "..index1)
    print("Item: "..itemName)
    print("Price: "..itemPrice)
    print("Weight: "..itemWeight)
    print("")
  end
end

The Output:

itemID: 0005
Item: Potion +100
Price: 300
Weight: 2

itemID: 0005
Item: Potion +100
Price: 300
Weight: 2

itemID: 0005
Item: Potion +100
Price: 300
Weight: 2

itemID: 1000
Item: Plus Bag Capacity
Price: 1000
Weight: 0

itemID: 1000
Item: Plus Bag Capacity
Price: 1000
Weight: 0

itemID: 1000
Item: Plus Bag Capacity
Price: 1000
Weight: 0

itemID: 0004
Item: Potion +20
Price: 300
Weight: 2

itemID: 0004
Item: Potion +20
Price: 300
Weight: 2

itemID: 0004
Item: Potion +20
Price: 300
Weight: 2

itemID: 0001
Item: Apple
Price: 100
Weight: 1

itemID: 0001
Item: Apple
Price: 100
Weight: 1

itemID: 0001
Item: Apple
Price: 100
Weight: 1

itemID: 0002
Item: Pine-Apple
Price: 250
Weight: 3

itemID: 0002
Item: Pine-Apple
Price: 250
Weight: 3

itemID: 0002
Item: Pine-Apple
Price: 250
Weight: 3

itemID: 0003
Item: Golden Tomato
Price: 500
Weight: 10

itemID: 0003
Item: Golden Tomato
Price: 500
Weight: 10

itemID: 0003
Item: Golden Tomato
Price: 500
Weight: 10

You never use the item or index2 variable you create by looping itemID. Is that necessary? This outputs each item once:

for index1, itemID in pairs(shop) do
  --for index2, item in pairs(itemID) do
    local itemName = itemID[1]
    local itemPrice = itemID[2]
    local itemWeight = itemID[3]
    print("itemID: "..index1)
    print("Item: "..itemName)
    print("Price: "..itemPrice)
    print("Weight: "..itemWeight)
    print("")
  --end
end
1 Like

This thing works, Sorry this is my first time using tables and stuffs so I kinda confuse a little.

Thank you!

That’s perfectly fine. Good luck in the future!

1 Like