Solved

inserting a random html snippet in an SSI tag?

Posted on 2000-04-20
2
312 Views
Last Modified: 2013-12-25
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
Comment
Question by:dibowkley
2 Comments
 
LVL 1

Accepted Solution

by:
chaduka earned 50 total points
ID: 2737302
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
 
LVL 84

Expert Comment

by:ozo
ID: 2738642
#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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now