×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

(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. ### RE: 2d-array in struct..please help! Do you really want to use a C-like struct....there are a few places where it may be better but...?? ...... maybe this will work for what you are doing... #!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! (OP) thx for the input.. but i already know how to use a normal 2d-array. The biggest problem is putting this 2d array in an object. I need to use objects for my program becuase i will need to put these objects in a queue. The example in my perl cookbook does not compile. It always tells me i have too many arguments in my array. ### RE: 2d-array in struct..please help! ' hope I was not insulting. It is sometimes difficult to know at what level one should respond to posts. Good Luck. keep the rudder amid ship and beware the odd typo ### RE: 2d-array in struct..please help! (OP) o no .. by all means i didnt mean to make it sound so offensive... hehe.. any advice i get is better than none.. so thanks for your time. ### RE: 2d-array in struct..please help! (OP) something else i just noticed about the struct is that i cannot store strings. The only thing it allows me to store are numbers. There must be someone out there who used struct before. Please help. ### RE: 2d-array in struct..please help! Hmmm... as entered above, I couldn't get your example to work. I kept getting "Too many args to board at test.pl line 9". This kind of suggested that too many items are being passed into your struct. 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

(OP)
your awesome. That really helped. It works!! you wont believe how much of a headache ive been having trying to get this to work. Thanks a lot.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!