Several comments:
- References are Scalars
- Perl has a few data types, the most common of which are lists, hashes, and scalars. A reference is a scalar. It "refers" to any data type. This is advantagious, because it allows you to put a non-scalar data type where you would normally store a scalar.
For example, A hash is a collection of scalars. You can't make $foo{Bar} hold a hash, but you can make it hold a reference to a hash. This means you can have nested data types.
Let's try a simple example for your program. Let's just say that you wanted to have multiple characters. You could have %characters hold them, with their name as the key, so that $characters{'Bob the Fighter'} would hold Bob's info. What is Bob's info? well, he'll have Attributes, Feats, Skills, Level, and so forth. Each of those could be a hash storing more detailed info. Let's initialize a sample:
$characters{'Bob the Fighter'} = { #The brackets create a reference t
+o an anonymous hash.
'Attributes' => { # We're nesting a second hash
'Str' => 10,
'Dex' => 10
}, #Done with that hash
'Level' => 1, #Storing a normal scalar
'Feats' => { #Another hash!
'Light_Armor' => 1 #Treating as a boolean
} #done with Feats Hash
}; #done with hash that is stored in $characters{'Bob the Fighter'
+}
To access some of Bob's data, we de-reference the reference with the arrow operator:
$character{'Bob the Fighter'}->{'Attributes'}->{'Str'} would equal 10.
References are key to any complicated data manipulation in Perl. I suggest experimenting.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|
|