Red Beginners Reference Guide

                                                         by Alan Brack      
                                                                                   This page was last updated:  23/08/2017
The idea of this guide is to give a quick reference to  gui level programmers.
It is not extensive and more will be added as and when I get chance.
My aim is to keep it simple. Remember it's not just about seasoned programmers.
An alphabetical list is included below for easy navigation, there's also a table of contents.
All examples should 'cut and paste' to the Red console, 
If you use an editor be sure to add the Red header:    Red [needs: 'view]

Red in Alphabetical order - Click on a  <<back to top>>  link to return here:

other stuff:

Just see the contents list below and click on what you need to know - click on a <<back to top>> link to return here:

Contents

  1. 1 Let's Start
  2. 2 First Script
  3. 3 ;;Here we use:     'below'    'text'   'font-size'   'font-color'   'at'   and   'bold'
  4. 4 ;;Here we use:    'flags'    'modal'  'resize'  'no-title'  'no-border'  'no-min'  'no-max'  'no-buttons'  'popup'
  5. 5 ;;Here we use:     'focus'  and   'set-focus'        ;; will set focus of face.
  6. 6 ;;Here we use:     'h1', 'h2', 'h3', 'h4', 'h5'       ;; Text styles.
  7. 7 ;;Here we use:     'Sort'           ;;Modifies the original, to ascending order.
  8. 8 ;;Here we use:     'insert'      ;; inserts a value at a series index.
  9. 9 ;;Here we use:     'Union'    ;;returns the union of 2 data sets.
  10. 10 ;;Here we use:     'hint'
  11. 11 ;;Here we use:    'false'    'true'          ;; boolean values.
  12. 12 ;;Here we use:    'offset'
  13. 13 ;;Here we use:    'rate'      ;; sets a timer for a face. 
  14. 14 ;;Here we use:    'on-down'   'on-up'   etc....   
  15. 15 ;;Here we use:    'pick'    'poke'     
  16. 16 ;;Here we use:    'change-dir'        change current folder.
  17. 17 ;;Here we use:    'suffix?'               Returns the suffix (extension) of filename or url, or none.
  18. 18 ;;Here we use:    'center-face'        centers a face inside its parent.
  19. 19 ;;Here we use:    'date'
  20. 20 ;;Here we use:    'call'        executes  a shell command or executable file.
  21. 21 ;;Here we use:    'empty?'          check if series is empty.
  22. 22 ;;Here we use:    'does'     defines a function with no arguments or local variables.
  23. 23 ;;Here we use:    'has'         defines a function with local variables only.
  24. 24 ;;Here we use:    'return'
  25. 25 ;;Here we use:    'panel'    'size'
  26. 26 ;;Here we use:    'last'             ;; returns the last value in the series.
  27. 27 ;;Here we use:    'length?'     ;; returns the number of values in the series.
  28. 28 ;;Here we use:    'title'     'across'     'check'    and   'radio'
  29. 29 ;;Here we use:    'backdrop'      'field'    and     'area'
  30. 30 ;;Here we use:    'image'
  31. 31 ;;Here we use:    'loop' 
  32. 32 ;;Here we use:    'break'
  33. 33 ;;Here we use:    'continue' 
  34. 34 ;;Here we use:     'read'    'foreach'    'find'    'rejoin'    'halt'
  35. 35 ;;Here we use:    'clear'      ;;to clear contents of a string / series.
  36. 36 ;;Here we use:    'function'    'func'    'exit'
  37. 37 ;;Here we use:    'any'     'all'  
  38. 38 ;;Here we use:    'print'     'prin'      'probe'     'space' 
  39. 39 ;;Here we use:    'same'
  40. 40 ;;Here we use:    'forever'    'unview'    'quit'    'if'    and     'print'   
  41. 41 ;;Here we use:    'foreach'    to read a block.
  42. 42  ;;Here we use:    'no-wait'    a refinement of view.     and    'wait'
  43. 43 ;;Here we use:    'save'    'load'    'write'   'read'    'delete'
  44. 44 ;;Here we use:    'suffix?'      To return extension of file.
  45. 45 ;;Here we use:    'load'     to convert a string to integer.
  46. 46 ;;Here we use:    'mold'            returns string representation of a value.
  47. 47 ;;Here we use:    'now/time'  'append'  and  'form'    
  48. 48 ;;Here we use:    'now/time'     to show the  hour, minutes and seconds. 
  49. 49 ;;Here we use:   'now/date'    ;; show the date.
  50. 50 ;;Here we use:    'request-file'  'request-dir'  'request-font'  pop-up system dialog boxes.
  51. 51 ;;Here we use:    'if error?'    and   'try'      we force an error by dividing by zero.
  52. 52 ;;Here we use:    'text-list'      'copy'      'data'
  53. 53 ;;Here we use:    'if'    'not'    and    'exists?'
  54. 54 ;;Here we use:    'do'     'uppercase'    and    'lowercase'
  55. 55 ;;Here we use:    '+'      '-'      '/'      '*'     'add'  'subtract'  'divide'   'multiply'   '%'
  56. 56 ;;Here we use:    'square-root'    'sqrt'
  57. 57 ;;Here we use:    'integer?'       Returns true if an integer.
  58. 58 ;;Here we use:    'string?'    Returns true if a string.
  59. 59 ;;Here we use:    'system/view/screens'     to find the screen size.
  60. 60 ;;Here we use:    'either' 
  61. 61 ;;Here we use:    'as-pair'        Combine X and Y values into a pair.
  62. 62 ;;Here we use:    'until'
  63. 63 ;;Here we use:    'while'
  64. 64 ;;Here we use:    'base'  'draw'  'pen'  'circle'  'box'  'triangle'  'polygon'  'fill-pen'  'line-width'
  65. 65 ;;Here we use:    'tab-panel'
  66. 66 ;;Here we use:    'overlap?'  and  'within?'  .... Collision detection.
  67. 67 ;;Here we use:    'trim'     'trim/head'      'trim/tail'     'trim/all'    'trim/with'  
  68. 68 ;;Here we use:    'make-dir'      
  69. 69 ;;Here we use:    'what-dir'                ;;<--get the active folder.  
  70. 70 ;;Here we use:    'dir'         ;;<--lists the current dir. Or list the full  path.
  71. 71 ;;Here we use:    'case'    'case/all'      ;;<--evaluates a block.
  72. 72 ;;Here we use:    'sort'    'sort/reverse'
  73. 73 ;;Here we use:    'alter'     'find'   
  74. 74 ;;Here we use:   'change'    ;;<--changes a value in a string or block.
  75. 75 ;;Here we use:   'extract'    'extract/index'
  76. 76 ;;Here we use:   'split'      ;;<--break a string into pieces using delimiters.
  77. 77 ;;Here we use:   'remove'    'remove/part'
  78. 78 ;;Here we use:   'split-path'     ;;<--splits a path, returning path and target.
  79. 79 ;;Here we use:   'to-local-file'    'to-local-file/full'      'to-red-file'
  80. 80 ;;Here we use:    'text-list'    'pick'
  81. 81 ;;Here we use:    'drop-down'
  82. 82 ;;Here we use:    'drop-list'
  83. 83 ;;Here we use:    'slider'     'progress'     'on-change'
  84. 84 ;;Here we use:    'style'    'group-box'    'reduce'    'origin'     'attempt'
  85. 85 ;;Here we use:    'what'   'about'
  86. 86 ;;Here we use:    'unique'              removes all duplicates.
  87. 87 ;;Here we use:    'to-integer''     'to-string'      'to-binary'
  88. 88 ;;Here we use:    'ask'
  89. 89 ;;Here we use:    'zero?'        returns true if zero,  else its false.
  90. 90 ;;Here we use:    'blue'      plus a list of all alternative colours.
  91. 91 ;;Here we use:   'random'     'random/seed'   'random/only'
  92. 92 ;;Here we use:   'switch'     'switch/default'
  93. 93 ;;Here we use:   'max'     'min'
  94. 94 ;;Here we use:   '<'    '<='  '<>'    '='    '>'    '<='    
  95. 95 ;;Here we use:   'unless'
  96. 96 ;;Here we use:   'reduce'
  97. 97 ;;Here we use:   'skip'
  98. 98 ;;Here we use:   'to-percent'
  99. 99 ;;Here we use:   'remainder'    Returns what's left when one value is divided by another.
  100. 100 ;;Here we use:   'browse'
  101. 101 ;;Here we use:   'to-hex'
  102. 102 ;;Here we use:   'enbase'    'debase'    'to string!'
  103. 103 ;;Here we use:   'take'   'take/last'    'take/part'
  104. 104 ;;Here we use:   'font-color'    'font-name'    'font-size'    'italic'    'bold'
  105. 105 ;;Here we use:   'view/options'    'event/key'    'func'    'object'
  106. 106 ;;Here we 'interpret' and 'compile' from the Dos prompt.
more will be added as and when I get time.

Let's Start

Red [ ]  - This is the first line of a Red script and must exist for your script to work.
Here it is with other stuff added. Notice that you can change the default app 
icon by declaring it here (ensure you have your new .ico file in the folder). 
The 'needs: 'view' is also important if you want to compile your script.

Red [ title: "My Script" 
        author: "Your Name" 
        icon: %myicon.ico 
        needs: 'View

First Script

To create a Window or Form or whatever you want to call it, you use 'view [ ....blah blah blah.... ]'
The script here will create a window with a button on it, click the button to quit.
Also note:  The  semi-colon ;  is used to add comments to your scripts.

;;Here we use:  'view'    'size'   and    'button'
Red [needs: 'view]
View [  size 300x100                    ;;<-- size your window
         ;;;; this is a comment
button "Hello World" [quit]    ;;<-- display a button and do something when its clicked
          ]


;;Here we use:     'below'    'text'   'font-size'   'font-color'   'at'   and   'bold'

Red [needs: 'view]
View [ below                                          ;;<--This will make items appear below the preceding items, see also 'across'
text "Hello World"                                                    ;;<-- display the message on your form.
text bold font-size 14 "Hello World"                        ;;<-- change font size and display message.
text font-size 16 font-color red "hello world"          ;;<-- change font size, declare a colour and display message.
at 30x20 text "This text is positioned"                     ;;<-- place text where you want it.
at 100x20 button "Quit" [quit]                                 ;;<-- place button where you want it.
          ]

;;Here we use:    'flags'    'modal'  'resize'  'no-title'  'no-border'  'no-min'  'no-max'  'no-buttons'  'popup'

Red [needs: 'view]
View/flags [ h5 "Makes window modal" ] 'modal
View/flags [ h5 "Enable window resizing" ] 'resize
View/flags [ h5 "No window title bar text"  button "Ok" [unview] ] 'no-title
View/flags [ h5 "Remove a windows border" button "Ok" [unview]  ] 'no-border
View/flags [ h5 "Remove minimise button from title bar" ] 'no-min
View/flags [ h5 "Remove maximise button from title bar" ] 'no-max
View/flags [ h5 "Remove all buttons from title bar" button "Ok" [unview] ] 'no-buttons
View/flags [ h5 "Makes window a popup (Windows only)" ] 'popup
--------------------------------------

;; this will make window modal and remove min button and allow resize.
Red [ needs: 'view] 
flags: clear []
        append flags 'modal
        append flags 'no-min
       append flags 'resize
          view/flags  [text "Hello World" ] flags

;; or this will do the same thing
view/flags  [text "Hello World" ] ['modal 'no-min 'resize]


;;Here we use:     'focus'  and   'set-focus'        ;; will set focus of face.

;; in this example 'area' has focus.
Red [needs: 'view]
view [ area focus 200x200   f: field  80x25 ]
;;----------------------------------------
;;change focus with 'set-focus'.
Red [needs: 'view]
view [below a: area focus 100x100  f: field  80x25 
          button "field"  [set-focus f  f/color: orange a/color: white] 
          button "area"  [set-focus a  a/color: orange f/color: white] 
]


;;Here we use:     'h1', 'h2', 'h3', 'h4', 'h5'       ;; Text styles.

;;font sizes 32  26  22  17  13

Red [needs: 'view]
view [below h1 "Hello" h2 "Hello" h3 "Hello" h4 "Hello" h5 "Hello"]

;; see also 'text'

;;Here we use:     'Sort'           ;;Modifies the original, to ascending order.

name: ["Steve" "Susan" "Clara" "Alan"]
sort name
print name

age: [60 33 22 19]
sort age
print age

sort/reverse age
print age

;;Type   '? sort'   at the Red console prompt for full list of refinements.


;;Here we use:     'insert'      ;; inserts a value at a series index.

st:  "my world"
insert st  "Hello "
print st

;;insert refinements are:  /part  (limit number of  values inserted).
                                     /only  (Insert bock types as single values).
                                    /dup  (Duplicate inserted values).

;;note:  insert is destructive.

st:  "world"
st2: "Hello you"
insert/part st st2 6    ;; st will now hold 'Hello world'


;;Here we use:     'Union'    ;;returns the union of 2 data sets.

;; Imortant:  Duplicates are only included once.

union [1 2 55 4]  [1 2 3 4]    ;;result is [1 2 55 4 3]   ;;no duplicates.
union/case "abc" "Da"     ;;result is "abcD"   ;; case sensitive minus the duplicate  'a'


;;Here we use:     'hint'

Red [needs: 'view]
view [field 100x30  hint "Enter some text"]


;;Here we use:    'false'    'true'          ;; boolean values.

1 = 2            ;;<-- clearly false.
5 = 5.0         ;;<-- this is true.
"hello"  =  "HELLO"       ;;<-- also true.          ;;anyway, all is quite straight forward.

;;Here we use:    'offset'

Red [needs: 'view]
View [ t: text "Hello World"
          button "Move text" [t/offset: 20x30]       ;;<-- change offset of text.
        ]
---------------------------------------------------------
Red [needs: 'view]
v: view/no-wait [ h1 "Ok"
      ]  
v/offset: 20x20                                            ;;<-- change offset of  view window.


;;Here we use:    'rate'      ;; sets a timer for a face. 

;;A time event is generated at each tick
Red [Needs: 'View]
view [ f: h1 "Red Programming !!"   
rate 1 on-time [either f/text = "" [ f/text: "Red Programming !!" ] [f/text: "" ] ]
  ] 


;;Here we use:    'on-down'   'on-up'   etc....   

Red [Needs: 'View]
view [h1 blue font-color red "Red Programming !!" on-down [quit]      ;; click on text to quit

;;More on this here .... https://doc.red-lang.org/en/view.html#_event_names


;;Here we use:    'pick'    'poke'     

;;returns the value at the given index

n: [12 23 35 47 59 66]
p: pick n 4
print p

s: "abcdefg"
p: pick s 6
print p
-----------

;;replaces the value at the given index

n: [1 2 3 4]
poke n 3 9
print n

s: "abcde"
poke s 3 #"S"
print s


;;Here we use:    'change-dir'        change current folder.

change-dir   %c/users/myfolder/new          ;;<-- for example.

;;Here we use:    'suffix?'               Returns the suffix (extension) of filename or url, or none.

f: "myScript.red"

either suffix? f [ print "Yes"] [print "No"]

;;Here we use:    'center-face'        centers a face inside its parent.

Red [needs: 'view]
view/no-wait [size 800x600     txt: text font-size 20  "Hello World !"] 
center-face  txt                                                        ;;<-- center txt in the window.
do-events

;;Here we use:    'date'

Red [needs: 'view]
d: now/date
print d
print now/day
print now/month
print now/year

;;Here we use:    'call'        executes  a shell command or executable file.

;;;;; Works with ver. 'red-062.exe'  or later.
;;;; Refinements:    /wait /show /console /shell /input in /output out /error err

call/shell "explorer.exe"
call/shell "notepad.exe"

call "start/min cmd"         ;;<--  cmd prompt minimised.
call "start/max cmd"        ;;<-- cmd prompt maximised.

call "start mkdir myfolder"     ;;<-- create new folder.
call "start rename myfolder newfolder"    ;;<-- Rename the folder.
call "start rd newfolder"     ;;<--Remove/delete  folder.

call "start copy nul > myfile.txt"    ;;<-- Create an empty file.
call "start rename myfile.txt newfile.txt"   ;;<--Rename the file.
call "start copy newfile.txt mycopy.txt"     ;;<--make a copy of a file.

call "start/b del newfile.txt"    ;;<-- Delete file. Note 'start/b'  to close cmd window after execution.
call/shell "del newfile.txt"    ;;<--Alternatively, this gives same result - delete and close cmd.

call "start color fc"   ;;<- cmd colour.
call "start color 9 9"     ;;<--this should give a list of all the 'cmd' colour posibilities.

call "start echo Hello World !"     <--This will display the 'Hello World' message in the cmd window.
call "start  ECHO ^G"    ;;<--A sound on most pc's.

call "start date"     ;;<--The date.   
call "start time"     ;;<-- The time.    

call "start ver"     ;;<-- Gives the windows version.
call "start dir"     ;;<-- display the folder contents.


;;Here we use:    'empty?'          check if series is empty.

e:  [2]
either empty? e ["Yes Empty"] ["No not Empty"]
f:  "H"
either empty? f ["Yes Empty"] ["no not Empty"]
g: ""
either empty? g ["Yes Empty"] ["No not Empty"]


;;Here we use:    'does'     defines a function with no arguments or local variables.

helloo:  does [ print "Hello "  print "World !"]
helloo


;;Here we use:    'has'         defines a function with local variables only.

nmb:  has [n] [ n: 20 - 2] 
print nmb

;;Here we use:    'return'

;; try it and you'll get it.
view [
    text font-color red "hello"
    text font-color purple "world"
    return  return                                  ;;<-- return 
    text font-color blue "how are you ?"
    ]


;;Here we use:    'panel'    'size'

Red [needs: 'view]

view [title "MyPanel"  size 200x200
below
panel red [size 100x150 below text red "Panel" check red button "Quit" [quit]]
        ]

;;Here we use:    'last'             ;; returns the last value in the series.

last ["me" "you" "us" "them"]       ;;<-- will return 'them'

last [10 20 30 40]         ;;<-- will return 40.

;;Here we use:    'length?'     ;; returns the number of values in the series.

L: length? [10 15 20 25]        ;;<-- there are 4.
print L

s: length? "abcdefg"                ;;<-- there are 7.
print s


;;Here we use:    'title'     'across'     'check'    and   'radio'

Red [needs: 'view]
View [ title "My Program"                          ;;<-- show this in the title bar.
across                                ;;<-- this will make items appear across from the preceding one.
check "My check" font-color green   ;;<--display a check box with green text.
radio "My radio"  font-size 12          ;;<- display a radio button with text size 12.
]


;;Here we use:    'backdrop'      'field'    and     'area'

Red [needs: 'view]
View [ backdrop cyan                                                             ;;<-- background colour.
t: text cyan                                                                           ;;<-- make t hold text.   
name: field                                                                           ;;<--  create a field.     
button "<   Type something and click" [t/text: name/text]   ;;<-- make text show whats in field.
a: area                                                                          ;;<-- create an area.  'a' will equal area.
  button "<   Type something and click" [t/text: a/text]        ;;<-- make text show whats in area.
          ]
;;see examples on my 'Red Programming' page.

;;Here we use:    'image'

Red [needs: 'view]
View [ image %myImage.jpg         ;;<-- display image (make sure image file is in your folder).
          image %myImage.jpg 400x400                ;;<-- display image to your chosen size.
          at 300x200 image %myImage.jpg 50x50  ;;<-- place image where you want it and re-size it. 
         ]
View [ image %myImage.jpg [Quit]  ]                   ;;<-- click on image to quit.

;;Here we use:    'loop' 

Red [needs: 'view]
loop 4 [                     ;;<-- will loop 4 times
View [ text "Hello World !" ]
           ]        


;;Here we use:    'break'

Red [ ]
n: 0
loop 16 [ n: n + 1 
        if n > 10 [break]       ;;<-- break out of loop.
        print n
        ]      
        print "Ended using Break"

;;Here we use:    'continue' 

Red [ ]
n: 0
loop 16 [ n: n + 1 
        if n < 10 [continue]      ;;<-- continue from 10.
        print n
        ] 
;----------------------------
Red [ ]
n: 0
loop 16 [ n: n + 1
        if n > 10 [continue]      ;;<-- continue till 10.
        print n
        ] 


;;Here we use:     'read'    'foreach'    'find'    'rejoin'    'halt'

Red [] 
folder: read %.                                                ;;<-- read the default folder in.
count: 0
foreach file folder [                                        ;;<-- outer foreach loop to evaluate folder.
    foreach ext [".jpg" ".gif" ".png" ".bmp"] [     ;;<-- inner foreach loop to get image file extentions.
        if find file ext [
            print file                   ;;<-- and print them to console.
            count: count + 1      ;;<-- keep count.
        ]      ;;<-- end off  'if'  block.
    ]          ;;<-- end off inner 'foreach' loop.
]              ;;<--end of outer 'foreach' loop.
print rejoin [newline "Total Images: " count]    ;;<-- reduce and join the values and print.
halt         ;;<-- halts script.



;;Here we use:    'clear'      ;;to clear contents of a string / series.

s: "abcdefg"
clear s                                      ;;<-- clear / delete contents of s.                           
print s                                 

s: "you me us them"
clear find s  "us"                      ;;<-- clear / delete from  "us" .
print s

n: [1 2 3 4 5]
clear skip n 3                           ;;<-- clear / delete from 3.
print n

;;see also:  'find' and 'skip'


;;Here we use:    'function'    'func'    'exit'

;; both 'function' and 'func'  will define a function. 
;; where everything in 'function' body is local - and everything in 'func' body is global.
;; 'exit' will  exit a function/func returning no value.

Red [ ]
sums: function [a b] [n1: a + b 
                                print n1                     ;;<-- local
                                if a < 1 [exit]
                                if b < 1 [exit]
                                n2: a * b
                                print n2                     ;;<-- local
                                
sums 2 5                        ;;<-- will return 7 and 10.
sums 0 4                        ;;<-- will return only 4 before exiting the function. 

;----------------------------------------         
;; below is the same 'func' version (global).
Red [ ]
sums: func [a b] [n1: a + b 
                          print n1                     ;;<-- global
                          if a < 1 [exit]
                          if b < 1 [exit]
                          n2: a * b
                          print n2                     ;;<-- global
                           ] 
sums 2 5                        ;;<-- will return 7 and 10.
sums 0 4                        ;;<-- will return only 4 before exiting the function.
 

;;Here we use:    'any'     'all'  

;; 'any' will return true if at least one condition is met.
n: 2
v: 4
if any [n = 2   v > 5]  [print true]
;-------------------------------------
;; 'all' will return true if all conditions are met.
n: 2
v: 4
if all [n = 2  v = 4]   [print true]

;;Here we use:    'print'     'prin'      'probe'     'space' 

;; 'print' outputs to the console window ending with a new line.
;; 'prin'  does the same without ending with a new line.
;; 'space' inserts a space.
;; 'probe' prints in a molded form.
;; 'newline' is a char! with value of:  #"^/"

print "Hello"    print "World !"

prin "Hello" prin space prin "World !"

probe "Hello World !"


;;Here we use:    'same'

s: 5
n: 5
either same? s n [print "They are the same"] [print "Not the same"]

;; now try:
s: 8
n: 5
either same? s n [print "They are the same"] [print "Not the same"]


;;Here we use:    'forever'    'unview'    'quit'    'if'    and     'print'   

Red [needs: 'view]
a:  0                                                                                ;; <-- declare a  variable/pointer
forever [                                                                          ;;<-- start a forever loop.
a:  a + 1                                                                  ;;<--    a = a + 1
View [ text "constant loop"  button "Not a quit" [unview]]    ;;<-- unview window (not quit).
              if a > 6  [quit]                                                 ;; <-- if   'a'  more than 6 then quit.
print a                                                          ;;<-- this will print 'a' to the console window.
]


;;Here we use:    'foreach'    to read a block.

Red [needs: 'view]
foreach info ["Us"  "Them"  "Me"  "You"] [
View [ text info ]                                           ;;<-- display result in a view
]

;; see more  'foreach'  here.


 ;;Here we use:    'no-wait'    a refinement of view.     and    'wait'

Red [needs: 'view]
loop 5 [
view/no-wait [ text "loop"]     ;;<-- display your window without waiting.
wait 1                                     ;;<-- pause a sec.
unview                                    ;;<-- close last opened view.
]

;;Here we use:    'save'    'load'    'write'   'read'    'delete'

Red [needs: 'view]
view [
below
t: text
button "Save" [ save %myfile.txt "Some text"  t/text: "Saved"]   ;;<--Save to folder when button clicked.
  button "Load" [t/text:  load %myfile.txt]              ;;<-- load from folder when button clicked.
button "Write" [write %myfile2.txt "More text"  t/text: "Written"]      ;;<-- Write when clicked.
button "Read" [t/text: read %myfile2.txt]                                          ;;<--Read when clicked.
         ]
;;Check out the two files in your folder ....

;;Now delete them ....
Red [needs: 'view]
view [ 
        button "Delete" [ delete %myfile.txt  delete %myfile2.txt ]
        ]

;;Here we use:    'suffix?'      To return extension of file.

s: suffix?  %myscript.red        ;;<-- will return ' .red ' 
print s                                   



;;Here we use:    'load'     to convert a string to integer.

mystring: "22"                        ;; <-- a string.
probe mystring                      ;;<--show in console window.
mystring: load mystring         ;;<--Now mystring is an integer.
probe mystring                      ;;<--show in console window.

;;Here we use:    'mold'            returns string representation of a value.

Red [needs: 'view]
n: 44                   ;;<-- make n equal the number 44.
s: mold n             ;;<-- use 'mold' to get a string representation of the value.
view  [ text s ]      ;;<-- now we can display it.


;;Here we use:    'now/time'  'append'  and  'form'    

;;append: Appends a value to the tail of a series and returns the series head.
;;form: Converts a value to a string.
;;Here we use  'Append' and 'form' to make the string 'Time' and  the value 'now/time' live together in a new string variable.

tim: append form  "Time: " now/time     ;;<-- set tim variable with time
probe tim                                               ;;<--  show the result in the console window.

;;Here we use:    'now/time'     to show the  hour, minutes and seconds. 

;;with the use of  'append and form'.

Red [ "Time" needs: view]
t: now/time
hr: append form  "Hours: " t/hour                  ;;<-- get the hour
mn: append form  "Minutes: " t/minute          ;;<-- get the minutes
sc: append form  "Seconds: " t/second           ;;<-- get the seconds
tim: append form  "The Time: " now/time      ;;-- get the complete time
view [ title: "The Time"                                   ;;<--open a view and give it a title
      below 
      text font-size 16 hr                                 ;; <--display the hour
      text font-size 16 mn                               ;;<--display the minute
      text font-size 16 sc                                 ;;<--display the second
      text font-size 16 font-color red tim        ;;<-- display the time
]                                                                   ;;<-- end of view

;;Here we use:   'now/date'    ;; show the date.

d: now/date
print d

print now/time/day
print now/time/month
print now/time/year

;; July 07/2017  build or later for this.


;;Here we use:    'request-file'  'request-dir'  'request-font'  pop-up system dialog boxes.

request-file/file %myfile.txt             ;;<-- pop-up 'Open' dialog box asking for myfile.txt.
request-file/file/save %myfile.txt     ;;<-- pop-up a 'Save' dialog box  for myfile.txt
request-dir                                      ;;<-- pop-up a 'folder' dialog box.
request-font                                    ;;<-- pop-up a 'font' dialog box.

;;Here we use:    'if error?'    and   'try'      we force an error by dividing by zero.

if error? try [0 / 0] [view [text "Divide by zero error !"]]

;;to make things clear:  
;;the first set of square brackets hold the error expression,
;;and the second set of square brackets deal with it.  i.e.  if error? try [ blahh blahh] [do something here if it's an error]

;;Here we use:    'text-list'      'copy'      'data'

Red [needs: view]
view [
    text-list data ["Me" "You" "Us" "Them"]
]

;;Or copy a list 

Red [needs: view]
myData: copy ["Me" "You" "Us" "Them"]
view [
    text-list data myData
]


;;Here we use:    'if'    'not'    and    'exists?'

if not exists? %noFile.dat [print "File not in folder"]  

;;this prints a message to console  'if'  file is  'not'  in folder.

;;Here we use:    'do'     'uppercase'    and    'lowercase'

Red [needs: 'view]
view [ t: text
            do [t/text: uppercase "hello"]                        ;;<--  change 'hello' to uppercase.
         ]
view [ t: text "HELLO"
            button "change" [t/text: lowercase t/text]     ;;<--  change 't' to lowercase.
         ]


;;Here we use:    '+'      '-'      '/'      '*'     'add'  'subtract'  'divide'   'multiply'   '%'

a:  4 + 4    print a                        ;;<-- add  and print to console.
a: add 4 4    print a                      ;;<--gives same result
a:  5 - 2    print a                         ;;<-- subtract  and print to console.
a: subtract 5 2   print a                ;;<-- gives same result
a:  10 / 6   print a                        ;;<-- divide  and print to console.
a: divide 10 6   print a                 ;;<-- gives same result
a:  4 * 2   print a                          ;;<-- multiply  and print to console.
a: multiply 4 2  print a                 ;;<-- gives same result.

a: 13 % 6                           ;; goes 2, so 'a' will equal 1. 
print a

;;Here we use:    'square-root'    'sqrt'

s: square-root 100     ;; <--  will return the square root.

s: sqrt  100                ;; <--  will do the same.

;;Here we use:    'integer?'       Returns true if an integer.

integer? 6        ;; <--  will return true.
integer? 4.5     ;; <--  will return false.

n: 25
either integer? n [print "Yes, its an Integer"] [print "No, its not an Integer"]  ;; <-- will print 'Yes'

n: 23.5
either integer? n [print "Yes, its an Integer"] [print "No, its not an Integer"]  ;; <-- will print 'No'


;;Here we use:    'string?'    Returns true if a string.

string? "Hello"        ;; <--  will return true.
string? 44              ;; <--  will return false.

s: "Hello" 
either string? s [print "Yes, its a string"] [print "No, its not a string"]  ;; <-- will print 'Yes'    

s: 44
either string? s [print "Yes, its a string"] [print "No, its not a string"]  ;; <-- will print 'No'


;;Here we use:    'system/view/screens'     to find the screen size.

Red [needs: 'view]
        scrn: system/view/screens                             ;; <-- get the screen size.
        s: append form "screen size = " scrn/1/size   ;;<-- make it view friendly.
        view [ text s  ]                                                 ;;<-- display it.

;;Here we use:    'either' 

a: 10
either a = 9  [print "a equals 9"]  [print "a does not equal 9"]

;;So if the condition is true  eval first(true) block  else   eval second(false) block. 
;;In this case 'a' does not equal 9 - so the second block is evaluated.


;;Here we use:    'as-pair'        Combine X and Y values into a pair.

x: 20
y: 10
p: as-pair x y                 ;;<-- make them a pair
print P     

;;Here we use:    'until'

Red [needs: 'view]
n: 0
until [
 n1: mold n
   view/no-wait [text n1 ]
   wait 1
   n: n + 1
   n > 5
]
print "Done"

;;Here we use:    'while'

c: 1
while [c  < 10] [ print  c: c + 1]     ;;<-- condition block and body block
print "done"


;;Here we use:    'base'  'draw'  'pen'  'circle'  'box'  'triangle'  'polygon'  'fill-pen'  'line-width'

;;Remember 'draw' is a massive dialect (triangle, box, circle, arc, curve, etc, etc, etc ....)
Red [needs: 'view]
;; circle
 view [base 200x200 draw [pen yellow circle 100x100 40]]
;; square
 view [base 200x200 draw [pen red box 60x60 140x140]] 
;; triangle
 view [base 200x200 draw [pen blue triangle 60x110 140x110 100x50]]
;; polygon 
 view [base 200x200 draw [pen cyan polygon 100x60 120x120  60x140]]
;; box with fill-pen 
 view [base 200x200 draw [fill-pen red box 60x60 140x140]] 
;; circle with fill-pen
 view [base 200x200 draw [fill-pen yellow circle 100x100 40]] 
;; triangle with fill-pen 
 view [base 200x200 draw [fill-pen blue triangle 60x110 140x110 100x50]]
;; line with line-width set to 10
 view [base 200x200 draw [line-width 10 pen blue line 40x100 150x120]]


;;Here we use:    'tab-panel'

   Red [needs: 'view]
   view [ 
    Title "Tab-Panel Example"
    tab-panel 200x100 [
        "Tab 1 " [at 30x30 text font-size 16 font-color red "Panel 1 ...."]
        "Tab 2 " [at 30x30 text font-size 16 font-color blue "Panel 2 ...."]
        "Tab 3 " [at 30x30 text font-size 16 font-color green "Panel 3 ...."]
                      ]
     ]


;;Here we use:    'overlap?'  and  'within?'  .... Collision detection.

if overlap? a b [print "collided"]     ;;;; <- where a and b are the colliding faces.
;-----------------------------------------------------------------
if within? point offset size [print "collided"] 
Note:  point is xy position- offset is offset of area - size is size of area


;;Here we use:    'trim'     'trim/head'      'trim/tail'     'trim/all'    'trim/with'  

;;;;  Removes spaces and/or chars from a string ;;;;

str: "    Hello World !.    "       ;;<-- str has 4 leading and 4 trailing spaces & 1 space in the middle.

trim str                                   ;;<-- remove leading and trailing spaces.
trim/head str                          ;;<-- remove leading spaces only.
trim/tail str                             ;;<-- remove trailing spaces only.
trim/all str                              ;;<-- remove all spaces.
trim/with str   "!."                    ;;<-- remove specified chars from a string.


;;Here we use:    'make-dir'      

make-dir %newDir/                               ;;<--create a new folder.

make-dir/deep %newDir/newSubDir/    ;;<--create a new folder with a sub folder.


;;Here we use:    'what-dir'                ;;<--get the active folder.  

dr: what-dir    
print dr  


;;Here we use:    'dir'         ;;<--lists the current dir. Or list the full  path.

dir                                         ;;<--simple - will list the current dir.
dir %/c/folder/                      ;;<--list the full path (where '%/c/folder/')  is your path.


;;Here we use:    'case'    'case/all'      ;;<--evaluates a block.

;;;; evaluates the first true condition only ;;;;
num: 200
case [
    num > 200 [print "num is more than 200"]
    num < 200 [print "num is less than 200"]
    num > 100 [print "num is more than 100"]
    num = 200 [print "num is 200"]
    true            [print "Yes its true"]
     ]
;---------------------------------------------------
;;;; evaluates all true conditions ;;;;
num: 200
case/all [
    num > 200 [print "num is more than 200"]
    num < 200 [print "num is less than 200"]
    num > 100 [print "num is more than 100"]
    num = 200 [print "num is 200"]
    true            [print "Yes its true"]
     ]


;;Here we use:    'sort'    'sort/reverse'

num: [5 3 9 7 6 4 8]
sort num                    ;;<--sort ascending.

sort/reverse num      ;;<--sort descending.

strng: ["S" "E" "D" "N" "F" "X" "W"]
sort strng                 ;;<--sort string ascending.

sort/reverse strng    ;;<--sort string descending.


;;Here we use:    'alter'     'find'   

a: "Hello"                ;;<--make 'a' = 'Hello'.    
    
alter a " World !"     ;;<-- appends  a   to  'Hello World !' (World ! is not there, so it is added)

alter a "!"        ;;<-- deletes  '!'  from  a  (when a value exists it is removed)

alter a "!"        ;;,-- puts  '!'  back  (when a value does not exist it is appended)

;--------------------------------------------

a: ["you" "me" "us"]
alter a "them"    ;;<--add  'them' 
alter a "us"        ;;<--remove  'us'
alter a "us"        ;;<--add 'us' back at the end.

find a "us"          ;;<--returns the series from the position value is found.
                                 See other 'find' refinements !   also see more  'find' here.
---------
txt: "Some text to look at"
if  find txt  "look" [print "found it"]
 --------


;;Here we use:   'change'    ;;<--changes a value in a string or block.

a: "you me us them"  
change a "yes"          ;;<--replace  the first 3 chars with 'yes'
print a                      ;;<--now print result to console.
;----------------------------------------
n: [11 12 13 14]
change n 22              ;;<--replace  11 with 22.
print n                       ;;<--print result to console.
;----------------------------------------
n: [12 13 14 15]
change n [88 99]
print n


;;Here we use:   'extract'    'extract/index'

extract [1 2 3 4 5] 2    ;;<--extracts every 2nd number from the block.
extract "Hello World" 3    ;;<--extracts every 3rd char from string.

extract/index "abcdefghijklm" 2 4    ;;<--start from offset 4 and extract every 2nd char.

g: extract/index "abcdefghijklm" 2 3
print g


;;Here we use:   'split'      ;;<--break a string into pieces using delimiters.

strng: "you and    me"
split strng " "


;;Here we use:   'remove'    'remove/part'

d: [2 4 6 8 10]
remove d                  ;;<--remove the first item from series.

s: "abcdefgh"
remove s                 ;;<--remove the first item from string.

remove/part d 2      ;;<--remove the first two items.
remove/part s 3       ;;<--remove the first three items.


;;Here we use:   'split-path'     ;;<--splits a path, returning path and target.

s: remove split-path  %desktop\ufo1.png     ;;<--using remove gives us the target.
print s


;;Here we use:   'to-local-file'    'to-local-file/full'      'to-red-file'

to-local-file   %/c/users/myfile.dat                    ;;<-- red file to local file format.
== "c:\users\myfile.dat"                          

to-local-file/full  %myfile.dat                             ;;<-- red file to full path.
== "c:\users\desktop\redthings\myfile.dat"

to-red-file "c:\users\myfile.dat"                         ;;<-- local file format to Red file.
== %/c/users/myfile.dat


;;Here we use:    'text-list'    'pick'

Red [needs: 'view]
view [title "Text-List"
    t: text "Click on a month ->"
    text-list 100x100 data[                         ;;<--start text-list
        "January" "February" "March" "April"
        "May" "June" "July" "August" "September"
        "October" "November" "December"]   ;;<--end text-list
    [t/text: pick face/data face/selected]     ;;<--what did we select
      ]                                                          ;;<--end view

-------------------------------------

;Pick from block
p: [a b c d e f g]
g: pick p 3
print g  

;Pick from string
s: "abcdefghijklm"
g: pick s 8                          
print g

;;see also here for more; 'pick'


;;Here we use:    'drop-down'

Red [needs: 'view]
view [title "drop-down"
     t: text "-->"
     drop-down "Choose one" data ["You" "Me" "Us" "Them"]
     on-change [t/text: pick face/data face/selected]
     ]


;;Here we use:    'drop-list'

Red [needs: 'view]
view [title "Drop-List"
    t: text "Choose and click ->"
    drop-list data ["You" "Me" "Us" "Them"] 
    select 1
    [t/text: pick face/data face/selected]
      ]  ;;<--end view


;;Here we use:    'slider'     'progress'     'on-change'

;; display a 'slider' and a 'progress' and use 'on-change' to update progress data.
Red [ needs: 'view]
      view [title "slider, progress bar"
        below   
        slider 100x20 data 10%  on-change [p/data: face/data]
        p: progress 100x20 10%        
    ]

;;Here we use:    'style'    'group-box'    'reduce'    'origin'     'attempt'

;; We create a 'group-box' with 'field's, 'button's, and 'text'. notice 'reduce' and 'attempt'.
;; 'reduce'  evaluates the block to be saved .... 'attempt' kills any error caused by loading a none field. 

 Red [needs: 'view]
    view [
    style txt: text                                                         ;; <-- sets a new style.
    group-box "My Group Box" 1  [origin 40x40         ;; <-- start of group-box and size it.
                txt "Name" name: field 200
                txt "Address1" addrs1: field 200
                txt "Address2" addrs2: field 200
    button "save" [save %mygroupbox.txt reduce [name/text addrs1/text addrs2/text ] t/text: "*Saved*"] 
    button "load" [attempt [set [nm ad1 ad2]  load %mygroupbox.txt]  
                attempt [name/text: nm] 
                attempt [addrs1/text: ad1]  
                attempt [addrs2/text: ad2]  
                t/text: " *Loaded* "] 
                t: text font-size 16 font-color red "                "
                                    ]
    ]


;;Here we use:    'what'   'about'

Typing 'what' at the Red console prompt will give you ....
.... brief information about all Red functions.

Typing 'about'  at the Red console prompt will give you ....
.... the Red version currently in use ....


;;Here we use:    'unique'              removes all duplicates.

unique "abcdabcdefg"               ;;<-- result will be   >> "abcdefg"

unique [1 2 3 4 1 2 3 4 5 6 7 8 ]      ;;<-- result will be  >> [1 2 3 4 5 6 7 8]



;;Here we use:    'to-integer''     'to-string'      'to-binary'

b: "Hello"
b: to-binary b             ;;<-- change to binary.  to binary!   does the same.
print b

b: to-string b             ;;<-- change back to string.   to string!   does the same.
print b

n: "44"
n: to-integer n             ;;<-- change to integer.   to integer!    does the same.
n: to-string n               ;;<-- and back to string.


;;also check out my 'Red Programming' page (example 18)

;;Here we use:    'ask'

ask "Enter your name: "             ;;<--copy this to the console.

n: ask "Enter your name: "         ;;<--now n will hold the string.

;;Here we use:    'zero?'        returns true if zero,  else its false.

n: 2
either zero? n  [print "yes its zero"]  [print "no it aint zero"]


;;Here we use:    'blue'      plus a list of all alternative colours.

Red [title: "MyColours" needs: 'view]
view [text font-color blue "Hello"        ;;<--set text colour.
         text orange "Hello"                    ;;<--set text background.
      ]

;;list of colours: 
;;aqua, beige, black, blue, brick, brown, coal, crimson, cyan, forest, gold, gray, green
;;ivory, khaki, leaf, linen, magenta, maroon, mint, navy, olive, orange, papaya, pewter
;;pink, purple, red.     

 ;<<Back to top>>;                                                                                                           

;;Here we use:   'random'     'random/seed'   'random/only'

Red []
random/seed now/time          ;;<-- seed the random generator.
loop 9 [
r: random 10                         ;;<-- get a random number.   
print r
        ]

Random/only ["one" "two" "three" "four"]    ;;<--picks a random value from a series.


;;Here we use:   'switch'     'switch/default'

;; in this example we use 'switch' to evaluate random numbers to find a match.
Red []
random/seed now/time
loop 8 [
r: random 5                                         ;; get a random number 
print r
switch r [1 [print "Number One"] 
              2 [print "Number Two"] 
              3 [print "Number Three"] 
              4 [print "Number Four"] 
              5 [print "Number Five"]
              ]
            ]
;-------------------------------------------
;; here 'switch/default' is used. 7 random numbers, with only 5 possible matches.
Red []
random/seed now/time
loop 8 [
r: random 7
print r
switch/default r [1 [print "Number One"] 
                          2 [print "Number Two"] 
                          3 [print "Number Three"] 
                          4 [print "Number Four"] 
                          5 [print "Number Five"]
                          ]
                             [print "No Number matched"]    ;; <-- default message.
            ]


;;Here we use:   'max'     'min'

max 420 610
min 420 610

max "abcd"  "dcba"
min "dcba"  "abcd"

max [1 2 3] [3 2 1]



;;Here we use:   '<'    '<='  '<>'    '='    '>'    '<='    

;; if any of these are not true they will return none.

if 2 < 3 [print "Yes"]                 ;;<-- will print message as it is true.

a: 3
if a <= 3 [print "Yes"]             ;;<-- will print message as it is true.

a: 3
if a <> 2 [print "yes"]             ;;<-- will print message as it is true.

a: 3
b: 3
if a = b [print "Ok, Yes"]       ;;<-- will print message as it is true.

a: 3
if a > 4 [print "ok"]        ;;<-- will return none. 

a: 3
either a <= 2 [print "yes its less than or equal to"] [print "no its not"]

s: "yes"
either s = "yes"  [print "It's true"] [print "It's false"]

;;Here we use:   'unless'

a:   5
unless a > 5 [print "Not more than 5"]    ;;<-- if not more than 5, print message to console.



;;Here we use:   'reduce'

;; returns a copy of the block after evaluating its expressions.
r: reduce [2 + 4]      ;;<-- puts 6 into a block.

r: reduce [2 + 4  3 + 4]    ;;<-- puts 6 and 7 into a block.


;;Here we use:   'skip'

s:  "abcdefg"  
skip s 2 

r: skip s 4
print r 



;;Here we use:   'to-percent'

p: to-percent  2       ;;<-- does what it says.

;;Here we use:   'remainder'    Returns what's left when one value is divided by another.

r:  remainder  10  4      ;;<-- 4  into  10 goes 2  and 2 left over.
print r

;;Here we use:   'browse'

browse  https://google.co.uk

browse  http://www.mycode4fun.co.uk/red-apps


;;Here we use:   'to-hex'

hx: to-hex 32        ;;<--  does what it says.


;;Here we use:   'enbase'    'debase'    'to string!'

;; enbase will create a binary string (base 64)
e:  enbase  "Hello"

d: debase  e       ;;<-- to binary.

to string!  d       ;;<-- and back to a string.

;;Here we use:   'take'   'take/last'    'take/part'

;; just try it and you'll get it.

t:  [12 13 14 15 16 17]

take  t

take/last t

take/part  t  3


;;Here we use:   'font-color'    'font-name'    'font-size'    'italic'    'bold'

Red [needs: 'view
         title: "fonts"
      ]
view [below
      text italic "Hello"
      text bold "Hello"
      text font-color purple underline "Hello"
      text yellow "Hello"
      text font-color red "Hello"
      text font-size 16 "Hello"
      text font-name "comic sans ms" font-size 14 italic "Hello"
      text italic bold font-color green font-size 16 "Hello"
      ]


;;Here we use:   'view/options'    'event/key'    'func'    'object'

;; detect keypress:    'x'    'q'    'space bar'....    should be easy to adapt for other keys.
Red [ needs: 'view]
;;;; get a keypress ;;;;
view/options [ t: text "hit 'space', 'x', or 'q' keys !"][
            actors: object [on-key: func [key event] [
            if event/key = #"x" [t/text: "'x' key pressed."]
            if event/key = #"q" [t/text:  "'q' key pressed."]
            if event/key = #" " [t/text: "'space bar'pressed."] 
         ]  ]  ]

;;  see also 'function'  for a better example of 'func'



;;Here we 'interpret' and 'compile' from the Dos prompt.

;;note: 'red.exe' should be the red version you have in your folder.
> red.exe  myscript.red              ;;<-- will interpret script, where 'myscript.red' is your script.
> red.exe  -c  myscript.red                   ;;<-- will compile script to Dos.
> red.exe  -c  -t windows myscript.red         ;;<-- will compile script to Windows.

;;See my     'AutoRed'     app to make this much easier.
;;Or use my  'RedEd'   text editor with interpret and compile options.
;;Both can be found on this page:   'Red Apps' 


Development Mode and Release Mode.
With regard to compilation of Red scripts. hope this is helpful.
At this point there is 'Development Mode' and 'Release mode'
'Development Mode' is the default. The command line would be: 'red.exe  -c  yourScript.red'  
The first time you try it it will build the 'libRedRT.dll' in your folder. After that you'll get lightning 
fast compiles, but remember the resulting exe will require the  'libRedRT.dll' to work.  
'Release Mode' generates a 'stand alone' no need for 'libRedRT.dll' Command line will be:  'red.exe  -c -r  yourScript.red'  
The compile time is a lot longer in release mode. 
Note also that a 'release mode' -r compile will be required in certain circumstances ....  i.e. when using  #include .... etc ....


Will be adding more soon ....