rand — return random element from an arbitrarily-separated list
Attribute | Pos. | Req. | Default | Description |
---|---|---|---|---|
file | Yes | File to load elements from. | ||
separator |
[alt]
|
Separator to split elements on. | ||
interpolate | 0 | interpolate input? | ||
reparse | 1 | interpolate output? | ||
hide | 0 | Hide the tag return value? |
This tag read the list of elements, separated by separator , and returns one random element.
The list of elements can be either passed in from the file argument or it can be specified in-place, in the tag body.
If the file argument is specified, it takes precedence over the tag body content. Note that using large files can impact performance, since they are read in to memory before a random element is selected.
Example: Return one random word
Put the following on a test page:
[rand separator=" "] Foo Bar Baz Quux Toad Stool [/rand]
During the split operation (performed on the list to extract single elements), the whitespace is significant, so make sure you do not have excessive spaces around elements. If you do, and especially if the separator used is a space character itself, you will sometimes get empty elements in return.
Interchange 5.9.0:
Source: code/UserTag/rand.tag
Lines: 24
# Copyright 2002-2007 Interchange Development Group and others # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. See the LICENSE file for details. # # $Id: rand.tag,v 1.5 2007-03-30 23:40:57 pajamian Exp $ UserTag rand Order file UserTag rand posNumber 1 UserTag rand addAttr UserTag rand hasEndTag UserTag rand Version $Revision: 1.5 $ UserTag rand Routine <<EOR sub { my ($file, $opt, $inline) = @_; my $sep = $opt->{separator} || '\[alt\]'; $inline = ::readfile($file) if $file; my @pieces = split /$sep/, $inline; return $pieces[int(rand(scalar @pieces))] ; } EOR