Solved

inserting a random html snippet in an SSI tag?

Posted on 2000-04-20
2
316 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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