2d-array in struct..please help!
2d-array in struct..please help!
(OP)
i am trying to get a 2d array in a structure. Here is what i am doing which is not working.
use Class::Struct;
struct Puzzel =>
{
board => '@'
};
my $hi = Puzzel->new();
$hi->board ( ['1', '2' ],
['4', '5' ],
['7', '8' ]);
printf "%d", $hi->board(1);
Please help. Many thanks to he/she that can solve this problem.
use Class::Struct;
struct Puzzel =>
{
board => '@'
};
my $hi = Puzzel->new();
$hi->board ( ['1', '2' ],
['4', '5' ],
['7', '8' ]);
printf "%d", $hi->board(1);
Please help. Many thanks to he/she that can solve this problem.
RE: 2d-array in struct..please help!
#!perl -w
@junk = (['1', '2' ],['4', '5' ],['7', '8' ]);
# retrieving elements
printf "%d\n", "$junk[0][0]";
printf "%d\n", "$junk[0][1]";
printf "%d\n", "$junk[1][0]";
printf "%d\n", "$junk[1][1]";
printf "%d\n", "$junk[2][0]";
printf "%d\n", "$junk[2][1]";
# setting an element
$junk[1][1] = '9';
print "Changed second element of second pair to ";
printf "%d\n", $junk[1][1];
'hope this helps.
keep the rudder amid ship and beware the odd typo
RE: 2d-array in struct..please help!
RE: 2d-array in struct..please help!
Good Luck.
keep the rudder amid ship and beware the odd typo
RE: 2d-array in struct..please help!
RE: 2d-array in struct..please help!
RE: 2d-array in struct..please help!
In other words, in your struct you have defined board as having one element of an array type. When inserting values into the struct, you are trying to insert 3 arrays.
Bearing this in mind, the assignment then seems to be a little out of whack. Stripping it down to assigning one array, we end up with:
$hi->board( ['1', '2' ]);
I think what you should be putting is:
$hi->board( 0, 1);
$hi->board( 1, 2);
This will assing a value of "1" to the first (0) element in the board array. It will assign a value of "2" to the 2nd element (1) in the board array. Now this is sorted out, you should also be able to store strings in the array.
Putting this all together we end up with:
use Class::Struct;
struct Puzzel =>
{
board => '@'
};
my $hi = Puzzel->new();
$hi->board( 0, 1);
$hi->board( 1, 2);
printf "%d", $hi->board(1);
Knowing this, if you want to have the struct represent some kind of 2- or 3-d array, you could maybe create board1, board2, board3, etc. arrays in the Puzzel struct.
Anyway, hope this clears things up, and thanks for making me go and take a look at struct :) I think I'll stick to standard arrays, though...
BTW, it should be possible to set up your own module that would contain a "new" function that would create a new array as goBoating suggested. The function could return a reference to that array which is then used by your program. That goes a bit off topic, though, so we won't go there ;^)
--
0 1 - Just my two bits
RE: 2d-array in struct..please help!