Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Copying two-dimensional arraysby Not_a_Number (Prior) |
on May 17, 2003 at 20:14 UTC ( [id://258900]=perlquestion: print w/replies, xml ) | Need Help?? |
Not_a_Number has asked for the wisdom of the Perl Monks concerning the following question:
Hello, all. This is my first post, please don’t judge me too severely if I mess up :-) Here we go:
I have found out, after much headbanging and looking for errors in the wrong parts of my code, that: @y = @x; works differently for one- and two-dimensional arrays, presumably because the latter are arrays of references and, well, er... The difference is that changing an element in @x will not affect @y if both are 1D arrays, while for 2D arrays, doing something like:
$x[0][2] = ‘foo’ changes both arrays, as illustrated by: which outputs “foo”. This is where my code breaks:
As might|mightn’t be obvious from the above, I am endeavouring to write a crossword/scrabble type programme. This extract simulates my attempt to check whether the user-entered word (simplified here to @word) fits in the existing (already partially filled) grid, given the user-entered coordinates (simplified here to [0,0,h] where ‘h’ is horizontal). If the word fits with the letters already in the matrix, no problem, let’s change the grid to include it and move on (this works, as illustrated by changing $letter to 'A'). If the word doesn’t fit, however, I need to: (a) print a warning (easy); (b) redo (or whatever) a loop that doesn’t concern us here in order to get new user input; (c) crucially, recuperate my matrix as it was previously. That is to say, based on the above snippet, I want the first ‘line’ of @matrix2 to be ‘. . . . X .’ and not ‘F O O B X .’. So, on to my question (at last): - Is there an obvious workaround for this? I’ve scoured perllol, perldsc, perlreftut, and (to the best of my limited capacities) perlref; have I missed something? - Or should I pursue my current idea of looping over the same data twice (which seems a waste...), first to check validity and second to insert the new word? - Or should I use some other type of data structure, which would seem unfortunate, since an AoA appeared to be ideal for my purposes? Thank you, knowledgeable Monks, for your input. Dave
Back to
Seekers of Perl Wisdom
|
|