Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

inserting a random html snippet in an SSI tag?

I've got the basic framework down for selecting from a list and displaying a random piece of arbitrary code, but much to my chagrin it always picks the last of them instead of a random one.  I know it's probably nothing more than a misplaced bit of punctuation, but I'm pulling my hair out over it...here's the code:

#!/usr/bin/perl
# Define Variable

@includedhtml = ("<random bit of html #1>",
      "<random bit #2>",
      "<random bit #3>");

srand(time ^ $$);
$num = rand(@includedhtml); # Pick a Random Number
print "Content-type: text/html\n\n";
print $includedhtml[$num];
print "\n";

Thanks everybody!
0
dibowkley
Asked:
dibowkley
1 Solution
 
chadukaCommented:
I donno what's contained in the <random bit of HTML ##> part, but make sure they are well written and characters that need escaping are taken care of correctly.

See my alteration and another suggested solution to handle this product below:

#!/usr/bin/perl
# Define Variable

@includedhtml = ('<random bit of html #1>',
'<random bit #2>',
'<random bit #3>'); # Note the use of single quotes, double quotes are a bit pecky.

srand(); # The srand() func. uses time as a seed by default.

$num = int(rand(@includedhtml)); # Pick a Random Number
# the rand() function returns a floating point value. You need to get the integer part of this number for your app to know exactly what to pick from @includedhtml

print "Content-type: text/html\n\n";
print $includedhtml[$num];
print "\n";

--------------------------------------
I would suggest you do it like this:
1) Either have the bits of HTML in a file, as in:
 1:<htmltag>My text</htmltag>
 2:<htmltag2>My other text</htmltag2>
or put each bit of HTML in a seperate file e.g. 1.html will contain some HTML stuff, 2.html etc...
2) generate a random number using srand() and rand().
3) read the line or file corresponding to that number.

Note: seperate files will be a bit faster if your randmon bits count grow.

...or better still, try a database!!

Hope this helps, ...and have fun!!
0
 
ozoCommented:
#The only bit of punctuation that should be giving you trouble is the ^
srand(time + $$); #would be a bit better
#or in 5.004 or later you can add a # before that line
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now