Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^3: Creating a unique variable type (an object)

by tfredett (Sexton)
on Mar 25, 2013 at 20:10 UTC ( #1025375=note: print w/replies, xml ) Need Help??

in reply to Re^2: Creating a unique variable type (an object)
in thread Creating a unique variable type

I could do the idea of having the boolean in one element and having the int in the following. Frankly though, I find it easier to manage and think about if I could have them both the boolean and the int in the same element. Reasoning for this, is I will have to have the system calculate different locations in a later portion of the code, and to just find a single element via very simple arithmetic is trivial, compared to doing a similar calculation, but then having to worry considering the next element, which will in essence throw off my calculation.

What I mean by this is I have several subroutines that currently depend on a simple calculation, in this case, lets take an example of placing a queen on D6, well with my two-dimensional array, I will completely block out the vertical and the horizontal columns, but what about the diagonals? well by doing a simple addition or subtraction of 1 to the factor being used in this case (in this case its actually two factors, the D which is equal to 4, and another that 6) With this, I could do a simple subtraction on one and addition on the other or two additions or two subtractions until I run out of possible squares. In our example, to resolve the upper left portion of the diagonal, we would do a double subtraction of 1 until one of the two arrays runs out of elements, and so on. This is why I want to have both of these values in the same element, since if they happen to be in the following element, I can no longer use the method described above to calculate all the squares being threatened by that queen that has been placed, in this case at D6, since as I go in one direction, I may be accessing either the boolean or the int, and I can't be sure without modifying the math used. It is just easier in this case to have them both in the same element if possible. The only other solution which could be used, is to create two two-dimensional arrays, and while that would work, I feel it would be a waste of resources that could be easily avoided.

A rough diagram of the board setup with the Q being the queen and the x's being the squares under attack, has been created below.

1 2 3 4 5 6 7 8 A | | |x| | |x| | | B | | | |x| |x| |x| C | | | | |x|x|x| | D |x|x|x|x|x|Q|x|x| E | | | | |x|x|x| | F | | | |x| |x| |x| G | | |x| | |x| | | H | |x| | | |x| | |

Replies are listed 'Best First'.
Re^4: Creating a unique variable type (an object)
by osler (Sexton) on Mar 25, 2013 at 22:43 UTC

    I apologize for not following your explanation fully. Can you post some code with your subroutines?

    One thought, take a look at Scalar::Util at the dualvar function.

Re^4: Creating a unique variable type (an object)
by Lotus1 (Curate) on Mar 26, 2013 at 13:21 UTC

    What osler is suggesting is using an array of arrays or a hash of arrays. Each element would actually be an arrayref that refers to an array with just the two values in it. So you would have both things in each element of the main array. This would be a good introduction to references which you need to know before creating objects.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025375]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2018-04-21 08:50 GMT
Find Nodes?
    Voting Booth?