Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thank you for taking the time to type this all out for everyone and me. Have a cookie! Now, I have some questions, though they aren't about OO specifically.

  1. How would you control explosive growth with this? I could sneeze and come up with a new list of items that I would like to randomize. Do I just keep adding new things to the top under RandThing and maybe keeping a list in a comment? The current set up looks a bit bulky for all of the randomizing I would be doing with this. Below are two lists I came up with while sitting here, and I may actually use them some day.
    package RandWritingImplement; use parent -norequire, 'RandThing'; sub new { my ($class) = @_; my @values = qw(pencil pen marker crayon chalk); return $class->SUPER::new (@values); } package RandWritingSurface; use parent -norequire, 'RandThing'; sub new { my ($class) = @_; my @values = qw(paper parchment slate hide stone); return $class->SUPER::new (@values); }
  2. How would RandThing be modified to accept not only arrays but also hashes? There are times where the list comes from a single hash key, the list could be the hash keys, or all of the arrays in the hash jumbled into one large list. Below is a subroutine I wrote which does it all for me, except that it doesn't take direct input.
    sub _random { my ($list,$key) = @_; my @random_array; if (ref($list) eq "HASH") { if ($list->{$key}) { @random_array = @{$list->{$key}}; } elsif ($key eq 'keys') { @random_array = (keys %{$list}); } elsif ($key eq 'all') { @random_array = map(@{$list->{$_}},keys %{$list}); } else { die q(You need to select a key from the hash, "all" keys, or jus +t "keys".); } } elsif (ref($list) eq "ARRAY") { @random_array = @{$list}; } else { die q(What are you doing? You didn't enter a hash or an array.); } return $random_array[rand @random_array]; }

These are two issues that I don't see an answer for in the current set up. OO does sound appealing, however, it appears to be very bulky for even something as simple as randomizing a lot of lists.

Have a cookie and a very nice day!
Lady Aleena

In reply to Re: Not quite an OO tutorial by Lady_Aleena
in thread Not quite an OO tutorial by GrandFather

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 chilling in the Monastery: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found