[Date Index][Thread Index]
[Date Prev][Date Next][Thread Prev][Thread Next]

Re: Help needed: I want to choose a random link

On Fri, 15 Sep 2000, Jan Ulrich Hasecke wrote:

> Hi all!
> I think I am truly the only wml-users with no
> perl-knowledge. O.K. I've read the first chapter of the german
> Llama-Book. ;-)

> I want to make pages with random tipps to other pages. Like this: 
> |  Foo bar. But read this too!
> |  Link 1
> |  Link 2
> |  Link 3

I don't know if this is really what you want to do, but if how 
about this:

--- SNIP ---
<define-tag random-href>
<preserve filename>
<set-var %attributes>

	$filename = "<get-var filename>";
	open FILE, "<$filename" or die;
	@data = ();

	while (<FILE>) {
		my ($name, $href) = split /;/; 
		push @data, [ $name, $href ];

	close FILE;
	($name, $href) = @{$data[rand(@data)]};

	print "<a href=$href>$name</a>";


# Use like this:

<random-href filename="random.txt">
--- SNIP ---

random.txt has the format

First Random Link;http://www.ran.dom
Second Random Link;http://www.foo.bar

etc. You'd have to generate this list of possible link targets
by hand or using the find command or whatever.

Used like this, <random-href> generates a random <a href...> out
of the list in the file given in the filename attribute.
(Beware: the link is "random" in the moment of generation, but in the
generated HTML it is a plain hyperlink, of course.)

The above code is far from elegant (in fact, I never used it in practice, 
I wrote it right now), as it reads the list of URLs on every invocation;
for small lists this might be sufficient, though. If you faced efficiency
problems, you'd need one tag to initialize the @data array with the 
link list (everything but the last two lines of code) and one tag to emit
a random href (the last two lines). If you wanted to do that correctly,
you'd need a way to name different link lists and specify in the "generate
random link now"-tag which one of the link lists to use. (That's when
the Camel book enters the scene ;-) So let's stick with reading the one
global list every time.

But is this really what you want? If you want to give the user a different
link each time he or she clicks on the hyperlink, you'd probably need to 
use JavaScript or CGI scripts or something similar (i. e., some technique
that is able to generate a redirect or something on the fly) -
which would be out of the scope of this list, I assume.

Good luck,


-- Christoph Schmitz <cs@cs75.de> | http://www.cs75.de --
Yes, but what was she in prison for? I wondered. And then my Automated
Mental Plot Analyzer hummed to life ...  
           -- Roger Ebert on "The Spitfire Grill"
Website META Language (WML)                www.engelschall.com/sw/wml/
Official Support Mailing List                   sw-wml@engelschall.com
Automated List Manager                       majordomo@engelschall.com