Randomness
Aren't you wishing you could mix and match randomly so you too could get a job marketing vapourware ? Heh.
@cool=( "networkable directory services", "legacy systems compatible", "Mission critical, Business Ready", "Internet ready modems !", "J(insert your cool phrase here)", "y2k approved, tested and safe !", "We know the Web. Yeah.", "...the leading product in an emerging market." ); srand; print "How many phrases would you like (max ",scalar(@cool),") ?"; while (1) { chop ($input=<STDIN>); if ($input <= scalar(@cool) and $input > 0) { last; } print 'Sorry, invalid input, try again :'; } for (1..$input) { $index=int(rand $#cool); print "$cool[$index] "; splice @cool, $index, 1; }
A few things to explain. Firstly, while (1) {
.
We want an everlasting loop, and this one way to do it.
1 is always true, so round it goes. We could test $input
directly, but that wouldn't allow
last
to be demonstrated.
Everlasting loops aren't useful unless you are a politician being interviewed. We need
to break out at some point. This is done by the last
function.
When $input
is between 1 and the number of
elements in @cool
then out we go. (You can also
break out to labels, in case you were wondering. And break out in a sweat. Don't start now
if you weren't.)
The srand
operator initialises the random
number generator. Works ok for us, but CGI programmers should think of something different
because their programs are so frequently run (they hope :-).
rand
generates a random number between 0 and
1, or 0 and a number it is given. In this case, the number of elements of @cool
-1, so from 0 to 7. There is no point generating
numbers between 1 and 8 because the array elements run from 0 to 7.
The int
function makes sure it is an integer,
that is no messy bits after the decimal point.
The splice
function removes the printed
element from the array so it won't appear again. Don't want to stress the point.