android
  #1  
Old 05-12-2010, 01:54 PM
Donone Donone is offline
Junior Member
 
Join Date: May 2010
Posts: 59
Default How to copy part of an array please

I have a table set up as a two dimensional array let us say 10 x 10 and each element contains a number.
I want to fill a smaller array of say 5 x 5 with the numbers contained in a 5 x 5 section of the larger table.
I have tried the obvious as would be done with normal arrays in say Basic, but it gives the error that I am attempting to index a field ( a nil value)

Please can somebody help before I go bald.
Reply With Quote

Advertisement [Remove Advertisement]

  #2  
Old 05-12-2010, 01:58 PM
Tiwiz Tiwiz is offline
Junior Member
 
Join Date: May 2010
Posts: 5
Default

Can you post the part of code in wich you are trying to use the array, pls?
Reply With Quote

  #3  
Old 05-12-2010, 02:37 PM
Brett_val's Avatar
Brett_val Brett_val is offline
Senior Member
 
Join Date: Jan 2010
Location: The Netherlands
Posts: 636
Default

From the error message I guess that you have a variable/image/whatever named the same as something that's on the line that gives the error message.

Okay I know this sounds vague, and I'm not sure if this is the case, but check if you have named some variable the same as another thing.. for example, naming an image "button" gives an "attempt to call field 'event' (a nil value)" when you call button.click(). Ok it's not really the same as your error but I think it's about the same issue. It would indeed help if you posted the code.
Reply With Quote

  #4  
Old 05-12-2010, 03:00 PM
Donone Donone is offline
Junior Member
 
Join Date: May 2010
Posts: 59
Default

Thanks for responding. I can't really include all the code but here are the table declarations and the errored code.

The variable values are known to be correct.
The error
main.lua:90: attempt to index field '?' (a nil value)
Lua Run Error : attempt to index field '?' (a nil value)

Code:
--create grids

MiniGrid = {}          
for Col=1,kMagnify do
  MiniGrid[Col] = {}     -- create a new Col
  for Row=1,kMagnify do
    MiniGrid[Col][Row] = 0
  end
end

Grid = {}          
for Col=1,kCells do
  Grid[Col] = {}     -- create a new Col
  for Row=1,kCells do
    Grid[Col][Row] = 0
  end
end

--copy part of grid to minigrid

for Col=1,kMagnify do
for Row=1,kMagnify do

--copy to mini from grid line 90 follows

MiniGrid[Col][Row] = Grid[cX+Col*kCell+1][cY+Row*kCell+1]
end
end
Reply With Quote

  #5  
Old 05-12-2010, 04:37 PM
Tiwiz Tiwiz is offline
Junior Member
 
Join Date: May 2010
Posts: 5
Default

try replace
Code:
--copy part of grid to minigrid

for Col=1,kMagnify do
for Row=1,kMagnify do
with
Code:
--copy part of grid to minigrid

for Col=1,kMagnify-1 do
for Row=1,kMagnify-1 do
does it help?
Reply With Quote

  #6  
Old 05-13-2010, 02:11 AM
Donone Donone is offline
Junior Member
 
Join Date: May 2010
Posts: 59
Default

Thanks but no it doesn't. If I had been using 0 to 4 (kMagnify) then it would but I have four entries 1-4 and kMagnify is 4.

Any other suggestions please? Am I trying to change a key rather than data?
I must confess a little difficulty with tables.

Last edited by Donone; 05-13-2010 at 03:06 AM.
Reply With Quote

  #7  
Old 05-13-2010, 02:38 AM
Brett_val's Avatar
Brett_val Brett_val is offline
Senior Member
 
Join Date: Jan 2010
Location: The Netherlands
Posts: 636
Default

Maybe Grid[cX+Col*kCell+1][cY+Row*kCell+1] doesn't exist.

You've created Grid[1][1] till Grid[kCells][kCells] , and if you just calculate "cX+Col*kCell+1" I guess it's way higher than kCells or is maybe some weird fraction.

Edit: which values did you print and were nil?
Reply With Quote

  #8  
Old 05-13-2010, 03:16 AM
Donone Donone is offline
Junior Member
 
Join Date: May 2010
Posts: 59
Default

Thank you for your time in looking at this. You are correct in that Grid[cX+Col*kCell+1][cY+Row*kCell+1] does not exist. cX goes too high (85), wow.
The nils came from my printing the array content just outside the creating loop which meant I was checking one off the end as i,j had completed.

Now solved. Thanks for pointing me to it. The solution always seems to be something stupid added to blindness.
Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:21 AM.