Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2d-array in struct..please help! 1

Status
Not open for further replies.

oinkers

Programmer
Jun 14, 2000
23
0
0
US
i am trying to get a 2d array in a structure. Here is what i am doing which is not working.<br><br>use Class::Struct;<br>struct Puzzel =&gt;<br>{<br> board =&gt; '@'<br>};<br><br>my $hi = Puzzel-&gt;new();<br><br>$hi-&gt;board ( ['1', '2' ],<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['4', '5' ],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['7', '8' ]);<br><br>printf &quot;%d&quot;, $hi-&gt;board(1);<br><br><br><br>Please help. Many thanks to he/she that can solve this problem.<br>
 
Do you really want to use a C-like struct....there are a few places where it may be better but...??&nbsp;&nbsp;&nbsp;&nbsp;...... maybe this will work for what you are doing...<br><br><FONT FACE=monospace><br>#!perl -w<br>@junk = (['1', '2' ],['4', '5' ],['7', '8' ]);<br># retrieving elements<br>printf &quot;%d\n&quot;, &quot;$junk[0][0]&quot;;<br>printf &quot;%d\n&quot;, &quot;$junk[0][1]&quot;;<br>printf &quot;%d\n&quot;, &quot;$junk[1][0]&quot;;<br>printf &quot;%d\n&quot;, &quot;$junk[1][1]&quot;;<br>printf &quot;%d\n&quot;, &quot;$junk[2][0]&quot;;<br>printf &quot;%d\n&quot;, &quot;$junk[2][1]&quot;;<br><br># setting an element<br>$junk[1][1] = '9';<br>print &quot;Changed second element of second pair to &quot;;<br>printf &quot;%d\n&quot;, $junk[1][1];<br></font><br><br>'hope this helps. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
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.
 
' hope I was not insulting.&nbsp;&nbsp;It is sometimes difficult to know at what level one should respond to posts.&nbsp;&nbsp;<br><br>Good Luck.<br> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
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.
 
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.
 
Hmmm... as entered above, I couldn't get your example to work.&nbsp;&nbsp;I kept getting &quot;Too many args to board at test.pl line 9&quot;.&nbsp;&nbsp;This kind of suggested that too many items are being passed into your struct.<br><br>In other words, in your struct you have defined board as having one element of an array type.&nbsp;&nbsp;When inserting values into the struct, you are trying to insert 3 arrays.<br><br>Bearing this in mind, the assignment then seems to be a little out of whack.&nbsp;&nbsp;Stripping it down to assigning one array, we end up with:<br><FONT FACE=monospace><br>$hi-&gt;board( ['1', '2' ]);<br></font><br>I think what you should be putting is:<br><FONT FACE=monospace><br>$hi-&gt;board( 0, 1);<br>$hi-&gt;board( 1, 2);<br></font><br>This will assing a value of &quot;1&quot; to the first (0) element in the board array.&nbsp;&nbsp;It will assign a value of &quot;2&quot; to the 2nd element (1) in the board array.&nbsp;&nbsp;Now this is sorted out, you should also be able to store strings in the array.<br><br>Putting this all together we end up with:<br><FONT FACE=monospace><br>use Class::Struct;<br>struct Puzzel =&gt;<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;board =&gt; '@'<br>};<br><br>my $hi = Puzzel-&gt;new();<br><br>$hi-&gt;board( 0, 1);<br>$hi-&gt;board( 1, 2);<br><br>printf &quot;%d&quot;, $hi-&gt;board(1);<br></font><br>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.<br><br>Anyway, hope this clears things up, and thanks for making me go and take a look at struct :)&nbsp;&nbsp;I think I'll stick to standard arrays, though...<br><br>BTW, it should be possible to set up your own module that would contain a &quot;new&quot; function that would create a new array as goBoating suggested.&nbsp;&nbsp;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 ;^)<br><br> <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top