Hello cmikel, and welcome to the Monastery!

As NetWallah says, you need to chomp the keyboard input string before using it as a hash key. NetWallah’s proposed OO design, using a House class as a container for Room objects, is a good approach. However, your original design can also be made to work with a few tweaks:

#! perl use strict; use warnings; package Room { sub new { my $class = shift; my $self = { id => shift, name => shift, detail => shift, }; return bless $self, $class; } } my %houserooms = ( kitchen => Room->new(1, 'kitchen', 'sleek and modern'), den => Room->new(2, 'den', 'board games and more'), ); print "\nEnter room (or \"quit\"):\n"; chomp(my $roomcmd = <>); while ($roomcmd ne 'quit') { print $houserooms{$roomcmd}->{detail} // "$roomcmd not found"; print "\n\nEnter room (or \"quit\"):\n"; chomp($roomcmd = <>); }

Typical output:

18:22 >perl 607_SoPW.pl Enter room (or "quit"): kitchen sleek and modern Enter room (or "quit"): den board games and more Enter room (or "quit"): bedroom bedroom not found Enter room (or "quit"): quit 18:23 >

Note that an object is a blessed reference, so there is no need to use syntax like this:

%houserooms = ( kitchen => { House->new("1","kitchen","sleek and modern") }, ... );

which takes a House object (already a reference!), puts it into another (anonymous) hash, and stores a reference to that hash as the value corresponding to the “kitchen” key in the hash %houserooms.

See perlreftut, perlref, perlootut, and perlobj.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Use Objects as Hash Values? by Athanasius
in thread Use Objects as Hash Values? by cmikel

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, details, 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, summary, 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.