Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Is a DBPro data type like a hash in Perl?

Author
Message
roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 29th Apr 2010 05:09
I've gone through a few of the tutorials, and I feel I can understand everything I've seen so far, but I'm a little fuzzy on the "data type" variable type. From what I gather, it's basically like a hash in Perl, or an array that's indexed by names rather than by numbers. Is this a good comparison, or do I still not grasp how to use the "data type" variable type?

For example, the data structure from the Dark Principles parallax starfield tutorial had the following:



From this code, the "stars" array looks actually to be an array of hashes in Perl vernacular where each index holds one of the DBPro "data type" variable types containing 3 values (x, y, and speed). Would someone confirm my thinking?
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 29th Apr 2010 07:37 Edited at: 29th Apr 2010 07:48
I don't know perl... but I'm thinking the answer to your question is "yes". Still, a data type doesn't necessarily have to be declared as an array, for example:


[edit]
but actually it's better just to think of types as... well, data types, like integers or floats. EX:



roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 29th Apr 2010 16:59
Thanks for the reply, NeuroFuzzy, but I think that's where I'm getting confused. I've always known data types to store a single simple value like a string or a number, and I've always known things like arrays and what DBPro calls "types" as data structures because they hold more than one value. The structure you've created in your above example looks like what I would call a multi-dimensional associative array (associative array is another name for a hash in Perl).

Your last line, however, doesn't make sense to me. It seems that your variable "a.b.c" is ambiguous and without a name. Wouldn't you have to say something like this?

Xyus
15
Years of Service
User Offline
Joined: 5th Sep 2009
Location:
Posted: 4th May 2010 15:19 Edited at: 4th May 2010 15:26
Quote: "it's basically like a hash in Perl"


After looking at a bit of Perl Documentation, a hash from Perl looks more like a Dictionary from Python than a Data Type from DP (Although, I haven't programmed in Perl before, so I wouldn't know that for certain. I only skimmed the docs.)

I'm not sure whether it it will help you any, as I am unsure how Perl Objects work (And thus I know not whether terms like "Method", and "Property" mean anything to you), or whether you know C#/C++, but when I look at a Data Type, I think of it more like a C#/C++ Class stripped of methods (Leaving behind only the Properties)

Like:



[Edit] In my case, Neuro Fuzzy's logic makes sense, as in C#, base types are all Objects, and at the same time all objects are types. You can thus overload, for instance, the base Int32 class, and extend it. Useful.
roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 4th May 2010 23:18
Thanks for your input, Xyus, but I'm still confused with NeuroFuzzy's last statement:



What exactly is "a.b.c" referencing? I see that he defined the types (abcd, efgh, ijkl), but I don't see where he made an instance of the types to assign any values to them. And "a.b.c" seems ambiguous because all 3 types have an "a" attribute, and all 3 types have a "b" attribute. I just wanted to clarify if NeuroFuzzy's example was supposed to just be an illustration of how things would work, or if it was supposed to be a working example, because I can't get the example to compile.

A hash in Perl is just an associative array. Instead of a stack of values indexed by numbers, think of a stack of values indexed by names.



Any clarification would be greatly appreciated!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th May 2010 23:51
If you are used to perl then you'll find DBPro's structure types a little lacking:
* There is no built-in 'hash' type.
* Structures are fixed at compile time.
* Arrays cannot be embedded within arrays or structures.

The nearest that there are to a hashes in DBPro that I'm aware of is is my 'lookup' plug-in (which uses a binary tree, not a hash table), and maybe the Dark Data plug-in (which is file based).

There have also been one or two XML plug-ins in the past that could also provide this kind of functionality.

BTW, Neuro Fuzzy's example should have been something like this:


The only changes are to the last two lines.

roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 5th May 2010 00:28
Thanks for your input as well, IanM, I appreciate it very much. However, your example is also confusing to me. In your code, it looks to me like you make an instance of type "ljkl" called "MyVar" and attempt to assign rgb value 255,255,255 to the attribute named "c". But type "ljkl" doesn't have a "c" attribute. My guess is that this was just a typo, but I want to be extra clear on the correct usage. Thank you for your help!
Xyus
15
Years of Service
User Offline
Joined: 5th Sep 2009
Location:
Posted: 5th May 2010 00:29 Edited at: 5th May 2010 00:31
Quote: "BTW, Neuro Fuzzy's example should have been something like this:
"


As IanM says, you were correct about how it worked in your last post, neuro fuzzy forgot to declare a variable and sort of "invoked" the type itself, which isn't really possible. Sorry for not clearing that up, it slipped my mind.

[off topic]
Quote: "I think of it more like a C#/C++ Class stripped of methods "


It'd be cool if you could use encapsulated functions in types though (As if THAT isn't one of the most talked-about things here...)
[/off topic]
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th May 2010 00:54
@roswell1329,
Follow this in the code.

MyVar.a.b.c = ...

The variable MyVar is an instance of type ijkl. ijkl has a nested instance of type efgh named 'a'. That in turn has a nested instance of type abcd that is named 'b', and that finally has a integer value named 'c'.

Note that these aren't pointers or references, but are embedded within their encapsulating data structure. You could convert these directly to C or C++ like so:


roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 5th May 2010 01:57
Aha! The light just came on. I understand this now. These do operate similar to Perl hashes in their definition, but the main difference appears to be that I can't dynamically change the stack in a DBPro type during runtime the way I can a Perl hash -- adding and removing attributes on the fly. Many thanks to everyone for their patience with a slow learner.
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 5th May 2010 10:25
Quote: "As IanM says, you were correct about how it worked in your last post, neuro fuzzy forgot to declare a variable and sort of "invoked" the type itself, which isn't really possible. "

Oh, right, whoops... Sorry about that


IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th May 2010 20:42
Quote: "I can't dynamically change the stack in a DBPro type during runtime"

Which is why I implemented my lookup plug-in, so that this kind of thing could be emulated.

As a very simple example, here's some perl code:


... and here's the moral equivalent in DBPro with my plug-in:


I chose the dot as the separator, but you could choose any character you want.

Login to post a reply

Server time is: 2024-11-23 17:14:22
Your offset time is: 2024-11-23 17:14:22