What happens if you use the incorrect pairs functions in an array and Dictionary?

You can write your topic however you want, but you need to answer these questions:

  1. **What do you want to * I’m curious to find out what would happen if you used pairs in an array instead of ipairs and ipairs in a dictionary instead of pairs

  2. What is the issue? I’m not sure what would happen if you used a incorrect pairs function

  3. What solutions have you tried so far? researched on google found no results

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

in pairs

Hello this is a scripting question i have about pairs and ipairs i would like to know what happens if you use the incorrect pairs in a array and dictionary.

Hi! you could try and test it yourself. It would take a few mins to setup a dictionary and a table, and then you can run a test. :slight_smile:

1 Like

ipairs will not work on a dictionary as dictionaries are not iterable – they’re key-value pairs and don’t have a numerical indexer like tables do, i.e. dictionary[1] will not reference the first element of a dictionary, it will error. pairs() on an iterable will loop until the end of the table, whereas ipairs() will loop through it so long as there is a next value ahead of the current iteration. If that doesn’t make sense, here’s an example:

local array = {"A", "B", nil, "C", "D", "E"}

for k, v in pairs(array) do
	print(string.format("%d: %s", k, tostring(v)))

for k, v in ipairs(array) do
	print(string.format("%d: %s", k, tostring(v)))

Looks like it’d be the same, right? But, ipairs will loop as long there is a “next” value. Here’s what the snippet outputs:

  1: A
  2: B
  4: C
  5: D
  6: E
  1: A
  2: B

ipairs quits at iteration 2 because the next k,v pair is 3, nil so there is no “next value” after B.

1 Like
local NormalRainbow_Array = {

local NotnormalRainbow_Array = {

-- Normal Array with ipairs
for i,v in ipairs(NormalRainbow_Array) do
-- Not normal array with pairs when it's supposed to be ipairs
for i,v in pairs(NotnormalRainbow_Array) do

-- Results Test 

-- prints out the same order no changes

-- Normal Dictonary with pairs loop

local NotNormalDictionary = {
	Word1 = "Test1",
	Word2 = "Test2",
	Word3 = "Text3"

-- Not normal dictionary using pairs
for i,v in pairs (NotNormalDictionary) do

local NormalDictionary = {
	Test1 = "Text1 with no pairs",
	Text2 = "Text2 with no pairs",
	Text3 = "Text3 with no pairs" 

for i,v in pairs(NormalDictionary) do

-- results the ipairs Printed out the text out of order while the one with pairs printed out the text in order

-- but they both printed out no problem with the array

Heres the results i have recieved the ipairs and pairs in the array prints fine no diffrence
it’s not until you get to the dictionary part then you will notice that the dictionary prints the ipairs out of order