Skip to content

AdoScript Examples ​

This page contains examples of AdoScript programs.

Reverse Text ​

The code

asc
# ITEM "Reverse text..." modelling
CC "AdoScript" EDITBOX text:"Miss Moneypenny"
IF (endbutton = "cancel") {
  EXIT
}
SET rev:""
WHILE (LEN text) {
  SET rev:(rev + text SUB (LEN text - 1))
  SET text:(copy (text, 0, LEN text - 1))
}
CC "AdoScript" INFOBOX ("*** reverse ***\n" + rev)
# ITEM "Reverse text..." modelling
CC "AdoScript" EDITBOX text:"Miss Moneypenny"
IF (endbutton = "cancel") {
  EXIT
}
SET rev:""
WHILE (LEN text) {
  SET rev:(rev + text SUB (LEN text - 1))
  SET text:(copy (text, 0, LEN text - 1))
}
CC "AdoScript" INFOBOX ("*** reverse ***\n" + rev)

lets the user enter any text, calculates the reverse text and displays it in an InfoBox.

In order to calculate the reverse text there the following function can be defined:

asc
FUNCTION rev s:string
return:(cond (LEN s, rev (copy (s, 1, -1)) + s SUB 0, ""))
CC "AdoScript" INFOBOX (rev ("Napoleon"))
FUNCTION rev s:string
return:(cond (LEN s, rev (copy (s, 1, -1)) + s SUB 0, ""))
CC "AdoScript" INFOBOX (rev ("Napoleon"))

Quicksort ​

The procedure QUICKSORT sorts the signs contained in a string alphabetically. repltok is a support function, which replaces in a string a line with another one via a determined index. The PARTITION is a help procedure, which divides the lines (elements) contained in a string into two halves and a pivot element. However, in one half only the smaller, and in the other half only the bigger elements are the pivot elements.

asc
PROCEDURE QUICKSORT list:reference start:integer end:integer
{
  IF (start < end) {
    PARTITION list:list start:(start) end:(end) result:split
    QUICKSORT list:list start:(start) end:(split - 1)
    QUICKSORT list:list start:(split + 1) end:(end)
  }
}

PROCEDURE PARTITION list:reference start:integer end:integer
result:reference
{
  SETL pivot:(token(list, end, "\n"))
  SETL bottom:(start - 1)
  SETL top:(end)

  SETL done:0
  WHILE (NOT done) {
    WHILE (NOT done) {
      SET bottom:(bottom + 1)
      IF (bottom = top) {
        SET done:1
        BREAK
      }
      IF (token(list, bottom, "\n") > pivot) {
        SET list:(repltok(list, top, token(list, bottom, "\n")))
        BREAK
      }
    }
    WHILE (NOT done) {
      SET top:(top - 1)
      IF (top = bottom) {
        SET done:1
        BREAK
      }
      IF (token(list, top, "\n") < pivot) {
        SET list:(repltok(list, bottom, token (list, top, "\n")))
        BREAK
      }
    }
  }

  SET list:(repltok(list, top, pivot))
  SET result:(top)
}

FUNCTION repltok list:string index:integer newtok:string
return:(set(r, ""), set(i, 0),
  fortok(t, list, "\n",
    (set(r, r + cond(LEN r, "\n", "") + cond(i = index, newtok, t)),
    set(i, i + 1))),
  r)
PROCEDURE QUICKSORT list:reference start:integer end:integer
{
  IF (start < end) {
    PARTITION list:list start:(start) end:(end) result:split
    QUICKSORT list:list start:(start) end:(split - 1)
    QUICKSORT list:list start:(split + 1) end:(end)
  }
}

PROCEDURE PARTITION list:reference start:integer end:integer
result:reference
{
  SETL pivot:(token(list, end, "\n"))
  SETL bottom:(start - 1)
  SETL top:(end)

  SETL done:0
  WHILE (NOT done) {
    WHILE (NOT done) {
      SET bottom:(bottom + 1)
      IF (bottom = top) {
        SET done:1
        BREAK
      }
      IF (token(list, bottom, "\n") > pivot) {
        SET list:(repltok(list, top, token(list, bottom, "\n")))
        BREAK
      }
    }
    WHILE (NOT done) {
      SET top:(top - 1)
      IF (top = bottom) {
        SET done:1
        BREAK
      }
      IF (token(list, top, "\n") < pivot) {
        SET list:(repltok(list, bottom, token (list, top, "\n")))
        BREAK
      }
    }
  }

  SET list:(repltok(list, top, pivot))
  SET result:(top)
}

FUNCTION repltok list:string index:integer newtok:string
return:(set(r, ""), set(i, 0),
  fortok(t, list, "\n",
    (set(r, r + cond(LEN r, "\n", "") + cond(i = index, newtok, t)),
    set(i, i + 1))),
  r)

It is possible to call QUICKSORT for example this way:

asc
SET text:("Caesar,Nero,Titus,Trajan,Konstantin,Augustus")
SET text:(replall(text, ",", "\n"))
QUICKSORT list:text start:0 end:(tokcnt(text, "\n"))
CC "AdoScript" INFOBOX (text)
SET text:("Caesar,Nero,Titus,Trajan,Konstantin,Augustus")
SET text:(replall(text, ",", "\n"))
QUICKSORT list:text start:0 end:(tokcnt(text, "\n"))
CC "AdoScript" INFOBOX (text)