Jump to content




For Loop, returning value in each line


  • You cannot reply to this topic
6 replies to this topic

#1 Hayden_Almeida

  • Members
  • 49 posts

Posted 26 October 2014 - 05:01 AM

[SOLVED]
Hello everyone, my problem is:

after using the "for k, in pairs ..."

its returns to me

10
1
15

MEANING i have: 10 Itens on Slot1, 1 Iten on Slot 2, 15 Itens on Slot 3.

Now i need to PLUS these numbers and Print the result! (10+ 1+ 15 = 26)

IMPORTANT PART OF THE CODE:

for k, detalhes in pairs(Details) do
	if detalhes.Name == "Block of Iron" then
	local b = detalhes.Size
	print(B)/>/>
	end
end

Edited by Hayden_Almeida, 26 October 2014 - 05:38 AM.


#2 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 26 October 2014 - 05:09 AM

What is the table Details?

local c = 0
for k,detalhes in pairs(Details) do
  if detalhes.Name == "Block of Iron" then
    local b = detalhes.Size
    c = c + b --#Adding the number to a number beforehand.
  end
end
print(c)


#3 Hayden_Almeida

  • Members
  • 49 posts

Posted 26 October 2014 - 05:13 AM

View PostDragon53535, on 26 October 2014 - 05:09 AM, said:

What is the table Details?

local c = 0
for k,detalhes in pairs(Details) do
  if detalhes.Name == "Block of Iron" then
	local b = detalhes.Size
	c = c + b --#Adding the number to a number beforehand.
  end
end
print(c)

It returns how many slots is used in a chest. But i think its not useful here.

Your code, does not work :/

Edited by Hayden_Almeida, 26 October 2014 - 05:14 AM.


#4 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 26 October 2014 - 05:21 AM

It should as i copied your code exactly. try replacing
local b = detalhes.Size
--#With
local b = tonumber(detalhe.Size)


#5 Hayden_Almeida

  • Members
  • 49 posts

Posted 26 October 2014 - 05:25 AM

View PostDragon53535, on 26 October 2014 - 05:21 AM, said:

It should as i copied your code exactly. try replacing
local b = detalhes.Size
--#With
local b = tonumber(detalhe.Size)
No changes :/

-----------------------------------
Now i Have 1 Item on Slot 1, And 2 Itens on Slots 2 (to be exactly: BLock of Iron)

My Entire Code with Peripheral (working normally)

local targets = prox.getTargets()
for name, basicDetails in pairs(targets) do

local moreDetails = prox.getTargetDetails(name)
  if moreDetails.RawName == "net.chest" then
   local Details = moreDetails.Slots
   print("----")
   for k, detalhes in pairs(Details) do
   --local a = textutils.serialize(Details)
   --print(a)
   if detalhes.Name == "Block of Iron" then
	local b = (detalhes.Size)
	print(B)/>/>
   end
(dont care about the "end" to close the "ifs")

Its Returning in the Screen:
1
2

Edited by Hayden_Almeida, 26 October 2014 - 05:26 AM.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 October 2014 - 05:27 AM

Best I can make out, Dragon's original code should function correctly.

View PostHayden_Almeida, on 26 October 2014 - 05:13 AM, said:

Your code, does not work :/

That statement is not helpful. What does it do? How is that different from what you want it to do?

#7 Hayden_Almeida

  • Members
  • 49 posts

Posted 26 October 2014 - 05:37 AM

View PostDragon53535, on 26 October 2014 - 05:09 AM, said:

What is the table Details?

local c = 0
for k,detalhes in pairs(Details) do
  if detalhes.Name == "Block of Iron" then
	local b = detalhes.Size
	c = c + b --#Adding the number to a number beforehand.
  end
end
print(c)

ITS WORKS!
SORRY , because i dont look at the print ( c ) at the final of the "END" closing the for loop :P

Thank you GUYS!

Edited by Hayden_Almeida, 26 October 2014 - 05:38 AM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users