User:Pete Olihenge

From Second Life Wiki
Jump to navigation Jump to search

LSL functions

No copyright, no attributions, no comebacks.

Random negative chat channel

This LSL function should, according to my calculations, return a random negative integer in the range -2,000,000,000 to -1,000,000, suitable for use as a dialog chat channel. <lsl>integer GetDialogChannel () {

   return (integer) llFrand (-1999000001.0) - 1000000;

}</lsl>

Ordinal suffix

This LSL function takes an integer and returns a string that is the integer with its ordinal suffix ("st", "nd", "rd" or "th") appended. <lsl>string OrdinalSuffix (integer number) {

   string result = "th"; //for all except 1s, 2s and 3s, but including 11, 12 and 13
   integer value = number % 100; //just the 10s
   if (value < 4 || value > 20) //exclude the teens (and their neighbours)
   {
       value = value % 10; //just the units
       if (value == 1) result = "st"; //*1st
       else if (value == 2) result = "nd"; //*2nd
       else if (value == 3) result = "rd"; //*3rd
   }
   return (string) number + result;

}</lsl>

Format a metric measure to Imperial (or US) units, with fractional inches

This LSL function takes a float value representing a measure in metres and returns a string representing the equivalent Imperial (or US) measure in feet and inches, rounded to the nearest quarter inch, and expressed using the feet and inches symbols ' and ", and the fractional characters ¼, ½ and ¾. <lsl>string FormatImperial (float metres) {

   string result = "";
   float inches = metres / 0.0254;
   integer whole_inches = (integer) (inches + 0.125);
   float part_inches = inches - (float) whole_inches;
   integer whole_feet = whole_inches / 12;
   whole_inches = whole_inches % 12;
   if (whole_feet) result += (string) whole_feet + "' ";
   if (whole_inches) result += (string) whole_inches;
   else if (part_inches < 0.125) result += "0";
   if (part_inches >= 0.625) result += "¾";
   else if (part_inches >= 0.375) result += "½";
   else if (part_inches >= 0.125) result += "¼";
   return result + "\"";

}</lsl>

Pluralise

This LSL function takes an integer and a string and returns a string that is the input string with a lower-case "S" appended if the integer is not equal to 1. <lsl>string Pluralise (integer number, string word) {

   if (number == 1) return word;
   else return word + "s";

}</lsl>

Indefinite article

This LSL function takes a string and returns it prepended by "an " if the input string begins with a vowel or by "a " if not. <lsl>string IndefiniteArticle (string word) {

   if (llSubStringIndex ("aeiou", llToLower (llGetSubString (word, 0, 0))) > -1) return "an " + word;
   else return "a " + word;

}</lsl>

Toggle text

This LSL function takes a Boolean integer and two strings and returns the first string if the integer represets TURE and the second if not. This is useful for including things like status information in the message parameter of llDialog. <lsl>string ToggleText (integer test, string if_true, string if_false) {

   if (test) return if_true;
   else return if_false;

}</lsl>