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

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