Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Daft text adventure project by swiftone
in thread Daft text adventure project by Tiefling

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-18 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found