How to check if the index matches with the values in ipairs

for i,v in pairs(Hello:GetChildren())
   if then -- I want to do a if check for whether or not the value is in the index here

   end
end

1 = Cat, 2 = Dog, 3 = Bat
but if i were switch the dog and the cat value then i would need a check to see if the index matches the value. is there anyway to go on doing this

1 Like

I’m confused, are you saying that you want to make sure 1 is always cat, 2 is always dog, and 3 is always bat?

1 Like
Hello = {
	[1] = "Dog";
	[2] = "Cat";
	[3] = "Bat";
}



for _,v in pairs(Hello) do
	local A = v.match(Hello[2], "Cat")
	if A then
		print(A)
	end
		
end

I’m not sure if this is what you mean but it works how I intend it to.

2 Likes

no i am trying to make sure the value equals the index they are in

Well, then you wouldn’t actually need to do that… Any programming language you could find that’s decent already does that, that’s how for loops work. But, if it matters that much, then you could just do this:

for i,v in pairs(Hello:GetChildren())
   if Hello[i] == v then

   end
end
4 Likes

does this also works if new index and values are being added to the table

1 Like

If you want to find a certain Value, Probably, but it isnt the best way of doing it.

1 Like
local Reference = {
   [1] = "Cat",
   [2] = "Dog",
   [3] = "Bat",
}

local randomArray = {"Dog", "Cat", "Bat"}

for idx ,name in randomArray do
   if Reference[idx] == name then
      print(name, "is in the correct index")
   end
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.