Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Creating a unique variable type (an object)

by Anonymous Monk
on Mar 25, 2013 at 01:07 UTC ( #1025214=note: print w/replies, xml ) Need Help??

in reply to Creating a unique variable type

Its called an object, read all about it in perlootut, Modern Perl

  • Comment on Re: Creating a unique variable type (an object)

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

    An object is certainly the answer to your question about how to create your own variable type, however you could also meet your needs by using the existing types if your program is a one off that only you will edit/maintain/read and you'd like to get to a solution prior to digging into perltoot for the first time (although you really should read this).

    You could use an arrayref for your variable and push the Boolean value onto the first spot in the array, then push the int onto the second. You then access these later knowing that the Boolean is always first and the int second. Incidentally this could be a model for your future object.

    my $foo = [ ]; push @{$foo}, 1; push @{$foo}, 23; my $boolean = ${$foo}[0]; my $int = ${$foo}[1];

      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| | |

        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.

        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.

Re^2: Creating a unique variable type (an object)
by tfredett (Sexton) on Mar 25, 2013 at 02:05 UTC
    Thank you for that, the name definitely helped, I guess I just had a brain fart if you will, but my issue is now resolved. Thank you again.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025214]
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-09-20 09:28 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (234 votes). Check out past polls.