Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
So I'm determined to do it the hard way, so that at the end (if there is an end),

Doing it the hard way is a time-honored tradition :) I just suggested you check out how the others work so that you can steal good ideas & interfaces.

is the long string in your penultimate line a single scalar for all purposes?

You mean $character{'Bob the Fighter'}->{'Attributes'}->{'Str'}? Yes, because in the end it is just a value stored in a hash, and only scalars can be thus stored. By dereferencing part of that mess, you can get a hash. For example, %{ $character{'Bob the Fighter'}->{'Attributes'} } is the Attributes hash. (The scalar value $characters{'Bob the Fighter'}->{'Attributes'} is a reference to a hash. the %{ } dereferences the reference.

Could I include lists in that hash by reference?

Absolutely. A reference to anything is a scalar, and a hash can store a scalar. An anonymous list can be created by [], or you can use the backslash reference. So:

$characters{'Bob the Fighter'}->{'Feats'}= [ 'Strong Spit', 'Perl Guru +']; # or @feats = ('Strong Spit', 'Perl Guru'); $characters{'Bob the Fighter'}->{'Feats'} = \@feats;
both work just fine. You can then access them like:
$characters{'Bob the Fighter'}->{'Feats'}->[1]; #is equal to 'Perl Guru'
Remember that although we're using a hash as our "base" variable here, we could use any other data type that is appropriate. A list could work:
$characters[3]->{'Attributes'}->{'Str'};
as could a scalar:
$characters->{'Bob the Fighter'}->{'Attributes'}->{'Str'};
(Assuming you had stored the data in that fashion.)

If, for example, $character{'Bob'}->{'Class'} holds 'Ftr', and $class{'Ftr'}->{'Base Attack'} holds 'Good' (to indicate that fighters have Good attack bonuses, which are generated by a short procedure in the combat module), what would be the code to retrieve Bob's Base Attack type?

Well, $class{$character{'Bob'}->{'class'}}->{'Base Attack'} would be equal to $class{'Ftr'->{'Base Attack'} (actually, it would _BE_ the same, since all that happens is $character{'Bob'}->{'Class'} resolves to 'Ftr') However, this assumes that Bob's class will ALWAYS have an entry in %class.

Storing feats, spells, etc. as booleans is not efficient, IMHO. I want to be able to brew a new feat and not have to go and set the bit in all the PCs.)

Just so you know, Perl handles non-existant hash keys quite nicely, and it's quite common to see code that would be like:

if($char{Bob}->{Feats}->{'Good With Pointy Objects'}){ Kill_Bad_Guy(); } else { Die_Hideous_Death(); }
If Bob doesn't have that feat, you don't have to set it to 0, you simply don't set it to a true value, and it will evaluate False. (Well, undefined, which in turn is false)

In reply to Re: Re: 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (1)
    As of 2020-12-05 06:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How often do you use taint mode?





      Results (63 votes). Check out past polls.

      Notices?