in reply to Re^2: Creating a random generator
in thread Creating a random generator

Thank you for the fabulous breakdown of issues you see. Let me see if I understand them.

The first item is that you suggested that I add -T to the shebang line. I was already told to add -w (after reading perlrun, I may possibly go to -W instead). Can both be added to the shebang?

Do the following two items duplicate the same thing? If they do, I would choose to keep the first.

use CGI; use CGI::Carp qw(fatalsToBrowser);
print "content-type: text/html \n\n";

I now only have the following in my code...

my @Dice = qw(1d4 1d6 1d8 1d10 1d12 1d20);

@Dice2 became redundant.

You expressed concern for my variable names, with the example being @Radius and $radius. I have been using the capitalized version for the array in the original TableSmith version, and the lowercase version for the phrase fragment for a while now. I can tell the difference.

All of the data in the script is static. As far as I know the arrays will only change if I think of something to add to them and write it into the code manually. The only thing that will change is the amount of times the user wants to run the script and the amount of times each random element will be called. The script as it is now is only a fragment of what is yet to come. I have a lot of markup to add to make this into a web page. I also have several other sub scripts to write that will be used by this one (but each of those must stand alone as well).

The original way I wrote the array dealing with the colors was...

my @Color = ( "<span class=\"mut\" style=\"color:#f00;\">red</span>", "<span class=\"mut\" style=\"color:#ff0;\">yellow</span>", "<span class=\"mut\" style=\"color:#0f0;\">green</span>", "<span class=\"mut\" style=\"color:#0ff;\">cyan</span>", "<span class=\"mut\" style=\"color:#00f;\">blue</span>", "<span class=\"mut\" style=\"color:#f0f;\">magenta</span>", "<span class=\"mut\" style=\"color:#fff;\">white</span>", "<span class=\"mut\" style=\"color:#000;\">black</span>" );
I thought that this would save me a few bytes...
my @Colors = ( "f00;\">red", "ff0;\">yellow", "0f0;\">green", "0ff;\">cyan", "00f;\">blue", "f0f;\">magenta", "fff;\">white", "000;\">black" ); my $Color = "<span class=\"mut\" style=\"color:#$Colors[rand @Colors]< +/span>";
But you think a hash is better?
my %Colors = ( red =>f00, yellow =>ff0, green =>0f0, cyan =>0ff, blue =>00f, magenta =>f0f, white =>fff, black =>000 );
How would you make that randomly selected and inserted into the following?
<span class="mut" style="color:(color code)">(color name)</span>
As soon as I get the entire script working, I will be getting rid of all unnecessary whitespace from it. I just hope that this doesn't go above the amount of bytes I have alloted for it.

Replies are listed 'Best First'.
Re^4: Creating a random generator
by Limbic~Region (Chancellor) on Sep 26, 2007 at 15:02 UTC
    Lady_Aleena,
    Can both be added to the shebang?

    Yes. With regards to -w, -W, and use warnings, You should read perlrun and compare it to warnings as typically they are not used in tandem.

    Do the following two items duplicate the same thing? If they do, I would choose to keep the first.

    No, they don't do the same thing. You should read the documentation for CGI and CGI::Carp to understand what both are doing and why you may need each of them.

    I now only have the following in my code...

    That's good. Removing redundant code makes the remaining code easier to understand. The point I was trying to make was more about how you were declaring the variables then having both though. The two lines of code I wrote to replace your original is much more succinct and clear. I know that you may not understand the map call but in the long run you will prefer to make perl do the work.

    You expressed concern for my variable names...I can tell the difference.

    Can the people you are asking for help tell the difference? If your project gains success and you choose to collaborate with others, will they be able to contribute? The point of writing maintainable code is very seldom about your ability to understand it.

    All of the data in the script is static....

    Then my point is very valid then. Separating your data from your code will make your code much easier to understand and manipulate. I would seriously consider storing all the data externally as I suggested. You will be amazed at how much easier your code is to read.

    The original way I wrote the array dealing with the colors was...I thought that this would save me a few bytes...But you think a hash is better?

    I think that you should use the data structure that best fits your data. If you want to have a relationship between a color name and the code to generate the color, then a hash better fits than an array. If you just want a list of values with no relationship then the array is fine. The important thing is choosing what best fits.

    How would you make that randomly selected and inserted into the following?

    I would probably do something like this (untested):

    sub get_random_color { my %color = ( red => f00, yellow => ff0, green => 0f0, cyan => 0ff, blue => 00f, magenta => f0f, white => fff, black => 000 ); my $choice = (keys %color)[rand keys %color]; return '<span class="mut" style="color:' . $color{$choice} . ';">' + . $choice . '</span>'; }

    Cheers - L~R