This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
martijnenco's profile picture

compare 2 arrays to make a new one.

Started by martijnenco, 30 March 2014 - 10:17 PM
martijnenco #1
Posted 31 March 2014 - 12:17 AM
Hello!
I'm new to the forums, and already it has been a great help for me but i need to ask something i couldn't find anywhere else..

I'm trying to compare 2 arrays with each other and make an other one with that information. If the value in array 'a' is the same as 'b[j]' then i want that NOT in my new array.. So in my new array are only all unique value's (that's the idea).

The steps I took in trying to achieve it was a for loop in 'b' within a for loop in 'a'. If there was a same value then he would over-write array 'c'[key] with a 'nil' value.
And at the final stage I made a for loop for the 'c' array and for every 'key' i would add a new value in array 'e'.

Problem:
The problem is that it does not work..
What does not work? I'm not sure if he do makes the 'c' or 'e' array correctly (although i do not think so (nearly certain)) the comparing does not work. It does not filter out the unique value's.. ..instead it just passes everything.

The comparing function and make the 'c'/'e' array function is:


function CompareMine()
r = 1
c = b
d = 1
e = {}
f = 0
for i in ipairs(a) do
  for j in ipairs(B)/>/>/> do
   if a[i][1] == b[j][1] and a[i][3] == b[j][3] then
	c[i] = {}
	c[i][1] = nil
	c[i][2] = nil
	c[i][3] = nil
   end
  end
end
for i in ipairs(B)/>/>/> do
  r = r + 1
end
for i = 1, r do
  if c[i][1] == nil then
  else
   e[d] = {}
   e[d][1] = c[i][1]
   e[d][2] = MaxAvgGround
   e[d][3] = c[i][3]
   d = d + 1
  end
end
for i in ipairs(e) do
  print(e[i][1].. ' '.. e[i][2].. ' '.. e[i][3])
  f = f + 1
end
print("available for mining: "..f)
end

The 'b' array he calculates itself.. using this code:


function CalculateMine()
b = {}
i = 1
p = 1
x = MineLocationX
z = MineLocationZ
xplus = x + PlusX
zplus = z + PlusZ
while calmine() do
  b[i] = {}
  b[i][1] = x
  b[i][2] = y
  b[i][3] = z
  i = i + 1
end
print("Number of possible shafts: " .. (i-1))
end
function calmine()
if (x + 5) <= xplus and i ~= 1 then
  x = x + 5
  return true
elseif i == 1 then
  return true
else
  x = MineLocationX
  if p == 1 then
   o = 0
  elseif p == 2 then
   o = 2
  elseif p == 3 then
   o = 4
  elseif p == 4 then
   o = 1
  elseif p == 5 then
   o = 3
   p = 0
  end
  x = x + o
  p = p + 1
  if (z + 1) <= zplus then
   z = z + 1
   return true
  else
   return false
  end
end
end

The 'a' array he get's from an other computer using rednet.receive()/send()


function Answer()
		print("turtle "..org_name.." asked for "..message)
		--name
		--x, y, z
		i = 1
		s = fs.open(message, 'r')
		while i ~= datacount do
				coords = s.readLine()
				if coords ~= nil then
						print("sended "..coords.." to "..org_name)
						rednet.send(org_name, coords)
						os.sleep(0.1)
				end
				i = i + 1
		end
		s.close()
end

Full code: client / server
Bomb Bloke #2
Posted 31 March 2014 - 08:30 AM
Funny thing about tables, when you assign them to a variable, that variable doesn't get the table attached to it: it gets a pointer to the table attached to it.

This means, for example, that the following code:

b = {}
c = b

… will lead to two variables ("b" and "c") pointing to the one single copy of the table in RAM. After doing so, setting eg c[1] to 3 would also result in b[1] being set to 3.

If you want to create a new, unique copy of a table (which is a mirror of the original, but after creation can be altered independently of the original), generally you'd iterate through everything in the source table and create copies of the values found in the destination one.
martijnenco #3
Posted 31 March 2014 - 01:49 PM
Ahh many thanks indeed! I did not know that, I will try that out later today and I will let you hear if it worked..

It worked, adjusted the syntax just a little more to be it to my liking..
If the hole program completely works out the way I want to I'll very proud of myself/upload a little video on youtube and post it here..