@DATABASE "ARB15"
@INDEX "ARB:Misc/Index.Text/Main"
@HELP "ARB:Misc/Help/Main"
@NODE "Main" "ARexx For Beginners - Article 15 - More about Strings"

@{B}@{U}@{JCENTER}AREXX FOR BEGINNERS

ARTICLE 15 - A BIT MORE ABOUT STRINGS

BY FRANK BUNTON@{UB}@{UU}

@{" COPYRIGHT © FRANK P. BUNTON 1995-1998 " LINK "ARB:Misc/Read_Me_First!!/Copyright"}
@{JLEFT}

I mentioned @{"Strings" LINK "ARB:Misc/Glossary/Strings"} in @{"Article 4" LINK "ARB:Articles_01-10/04.Numbers_&_Strings/Strings"} but did not go very fully into them as
it was desirable to learn more about ARexx first. Now, before we get into
talking about @{"Functions" LINK "ARB:Misc/Glossary/Functions"}, many of which use strings, we should learn more
about them.
@{JCENTER}

@{" Concatenation of Strings             " LINK "Concatenation"}
@{" The || Concatenation Operator        " LINK "||"}
@{" The `Space' Method of Concatenation  " LINK "Space"}
@{" The `None' Method of Concatenation   " LINK "None"}
@{" Hexadecimal & Binary Strings         " LINK "HexDec"}
@{" Escape Sequences For Special Effects " LINK "Escape"}
@{" Escape Sequence Codes Available      " LINK "Available"}
@{" Putting Codes Into SAY Instructions  " LINK "Putting"}


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "Concatenation" "Article 15 - More about Strings - Concatenation Of Strings"

@{B}@{U}@{JCENTER}Concatenation of Strings@{UB}@{UU}
@{JLEFT}
"Concatenation" is one of those fancy words that finds its way into computer
jargon. It simply means "joining together".

With strings, this can be done with one of the three "Concatenation
Operators". These are:-

  @{" ||    " LINK "||"}   (Two vertical bars - shift \ on keyboard)
  @{" Space " LINK "Space"}   (i.e. as given by the space bar)
  @{" None  " LINK "None"}   (i.e. two strings with nothing between)

As these are non mathematical operators, they do @{B}not@{UB} have @{"priority" LINK "ARB:Articles_01-10/09.Arithmetic_Operators/Priorities"} numbers
associated with them.

A full list of operators is contained in @{"Appendix B" LINK "ARB:Appendices/App-B_Operators/MAIN"27} which covers Arithmetic,
Logical, Comparison and concatenation operators.

These concatenation operators are best explained by examples which I have
done under there own headings:-

  @{" ||    " LINK "||"}
  @{" Space " LINK "Space"}
  @{" None  " LINK "None"}


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "||" "Article 15 - More about Strings - The || Concatenation Operator"

@{B}@{U}@{JCENTER}The || Concatenation Operator@{UB}@{UU}
@{JLEFT}
This joins together two strings, or the string values held by symbols.
It can be used as illustrated with these examples:-


  SAY 'Test'||'Match'
  --> TestMatch


  x = 'Test'||'Match'
  SAY x
  --> TestMatch


  x = 'Test'
  y = 'Match'
  z = x||y
  SAY z
  --> TestMatch

Note that, if you wanted a space between the two words in any of the
examples you should use, for example,:-

  x = 'Test'
  y = ' Match'
  z = x||y
  SAY z
  --> Test Match


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "Space" "Article 15 - More about Strings - The `Space' Method Of Concatenation"

@{B}@{U}@{JCENTER}The "Space" Method of Concatenation@{UB}@{UU}
@{JLEFT}
This can be used as follows:-

  SAY 'Test' 'Match'
  --> Test Match


  x = 'Test' 'Match'
  SAY x
  --> Test Match


  x = 'Test'
  y = 'Match'
  z = x y
  SAY z
  --> Test Match

The space character between 2 strings, or two symbols holding string values,
joins them together with a space between them.


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "None" "Article 15 - More about Strings - The `None' Method Of Concatenation"

@{B}@{U}@{JCENTER}The "None" Method of Concatenation@{UB}@{UU}
@{JLEFT}
The "None" method simply means that there is @{B}nothing@{UB} between the two items
being joined together - no operator and no space!

For example:-

  x = 'Test '
  y = ' Today'
  z = x'Match'y
  SAY z
  --> Test Match Today

In this example, in the third line (starting "z ="), the string and the
two symbols at each end of it are right next to each other without any
space or other operator between them. Their values are therefore joined
together as the value of "z".

This method should be used with care as its use can become confusing and
can lead to errors. There are some ways (see below) in which it @{B}cannot@{UB}
be used.

About the only way you @{B}can@{UB} use the "None" method of concatenation is to
join a symbol holding a string value to an actual string contained within
quotes as demonstrated above.

Example @{"15-1" LINK "HexDec"51}, @{"15-2" LINK "Putting"25} and @{"15-3" LINK "Putting"45} all have illustrations of its use.

The "None" method @{B}cannot@{UB} be used to join strings all of which are enclosed
in quotes, as in:-

  x = 'Test''Match''Today'
  SAY x

  --> Test'Match'Today

You will recall that putting two quotes next to each other within overall
quotes results in one quote being displayed.

It @{B}cannot@{UB} be used when joining the values of two symbols together, as
in:-

  x = 'Test'
  y = 'Match'
  z = xy
  SAY z
  --> XY

In this example, the line "z = xy" does not recognise the "xy" as the
two separate symbols but as one new symbol named "XY".

To obtain the result that was really intended in the above example, you
would need to use:-

  x = 'Test'
  y = 'Match'
  z = x||y
  SAY z
  --> TestMatch


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "HexDec" "Article 15 - More about Strings - Hexadecimal & Binary Strings"

@{B}@{U}@{JCENTER}Hexadecimal & Binary Strings@{UB}@{UU}
@{JLEFT}
If a string (in quotes) is followed by "x" or "b" then that string is
considered to be a @{"hexadecimal" LINK "ARB:Misc/Glossary/Hexadecimal"} or @{"binary" LINK "ARB:Misc/Glossary/Binary"} number respectively. For
example:-

  'FF'x      is the hexadecimal number FF
  '11010'b   is the binary number 11010

Care must be taken to ensure that only legitimate hexadecimal (0-9,A-F)
or binary numbers (0-1) are used otherwise a program stoppage and error
message will result.

When used with the "SAY" command, these numbers will be converted to the
characters (if any) that the number represents as an @{"ASCII" LINK "ARB:Misc/Glossary/ASCII"} code. For
example:-

  SAY '61'x        --> a
  SAY '1100001'b   --> a

The ASCII code for "a" is decimal 97 or Hex 61 or Binary 110001.

In general, the beginner or average ARexx user would only use the "x"
option, not use the "b" option.

Multiple numbers can be included, as in:-

  SAY '416D696761204C6976657321'x
  --> Amiga Lives!

This results because the ASCII codes (in hexadecimal) represent the
characters shown below them in:-

   41 6D 69 67 61 20 4C 69 76 65 73 21

   A  m  i  g  a     L  i  v  e  s  !

While this feature is not really of much use for the normal characters
that can be entered via the keyboard, it @{B}can@{UB} be @{B}very@{UB} useful for sending
to the output window ASCII codes that are @{"not printable characters" LINK "ARB:Appendices/App-C_ASCII_Codes/NonPrintable"}. For
example:-

  '7'x   Sound Bell and/or flash screen
  '8'x   Back Space
  '9'x   Horizontal Tab (normally 8 spaces)
  'a'x   Line Feed
  'b'x   Vertical Tab (Upwards 1 line)
  'c'x   Clear window (if output to screen) or form feed (if to printer)
  '1b'x  Escape Code

Example15-1 (below) demonstrates the effects of these codes. You should
be able to follow it without explanation.

  /* Example15-1.rexx */

  Cls = 'c'x                        /* Clear screen */
  Cdown = 'a'x                      /* Cursor Down */
  Bell = '7'x                       /* Sound bell */
  Header = Cls'THIS IS A HEADING'   /* Put header on each page */

  SAY Header
  SAY Cdown'Window has been cleared by header and there is a blank line
      above me.'
  SAY '9'x'This starts 8 places out.'
  SAY '8'x'My First character is on the previous line.'
  SAY Bell'Bell or Screen flash with this line.'
  SAY 'This line will not appear.'
  SAY 'b'x'As it is covered with this one.'
  SAY 'This line is followed by' 'b'x
  SAY '9'x'9'x'9'x' This line.'

When RXed, this program would display the following text with the bell
sounding and/or the screen flashing as the 5th SAY is operated:-

  THIS IS A HEADING

  Window has been cleared by header and there is a blank line above me.
          This starts 8 places out. M
  y First character is on the previous line.
  Bell or Screen flash with this line.
  As it is covered with this one.
  This line is followed by This line.


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "Escape" "Article 15 - More about Strings - Escape Sequences For Special Effects"

@{B}@{U}@{JCENTER}USING ESCAPE SEQUENCES FOR SPECIAL EFFECTS@{UB}@{UU}
@{JLEFT}
In the list of codes @{"given earlier" LINK "HexDec"38} I mentioned the "Escape Code". This
is @{"ASCII Code" LINK "ARB:Appendices/App-C_ASCII_Codes/NonPrintable"} 27 (decimal) or 1B (hexadecimal). To put it rather simply,
the escape code tells the system to "escape" from normal text display
and treat the characters that follow as some special instruction.

You can smarten up the output from the ARexx "SAY" instruction by using
escape sequences in the "SAY" statements. For instance, you can get bold,
underlined and italic characters as well as different colours.

@{B}@{U}What is an Escape Sequence?@{UB}@{UU}

To produce special effects we need to send special code characters to
the display window to tell it what to do. However, we also need to tell
the system that the codes are not characters to be displayed as such but
are special codes.

For example, to change to bold print, the codes are "[1m". However, if
we simply used:-

  SAY '[1mThis is in BOLD'

the display would show:-

  [1mThis is in BOLD

We need to send the "escape" code first. This escape code tells the system
that the following codes are not to be sent to the display window as such
but are to be interpreted as special control codes.

The escape code is decimal 27 or hexadecimal 1B.

If you press the ESC key and you see in the display window a reversed
open bracket (i.e. @{APEN 0}@{BG shadow}[@{APEN 1}@{BG Background}) then you can use the ESC key for your escape code.
Try it out at a CLI prompt. However, the "ED" program and a lot of word
processors and text editors do not allow this. They use the ESC key for
other purposes.

The alternative, which is probably best to get used to, is '1b'x. This
will send the escape code. It will work with all text editors.

So, to send the above mentioned sequence to make bold print, we would
use:-

  SAY '1b'x'[1mThis is in BOLD'

Now the '1b'x'[1m does @{B}not@{UB} appear on screen as such but this does:-

  @{B}This is in BOLD@{UB}

(I hope you can see that it is in bold type!!)

What I like to do is define a symbol named, for example, "esc" at the
start of my programs, as in:-

  esc = '1b'x

If you do this, you can then use:-

  SAY esc'[1mThis is in BOLD'

and get the same result.


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "Available" "Article 15 - More about Strings - Escape Codes Available"

@{B}@{U}@{JCENTER}Available Codes@{UB}@{UU}
@{JLEFT}
@{B}NOTE@{UB} - in these examples I will show the escape code as "esc".

@{B}NOTE@{UB} - the "0" in the following examples, e.g. in esc'[0m', is the number
       zero, not the letter O.

@{B}NOTE@{UB} - the "#" is NOT typed as such but substituted with a number.
       For example, where you see:-

           esc'[#u'

       you substitute a number such as "40" for the "#" to give:-

           esc'[40u'

The codes are:-

Resets        esc'c'     Clears window & resets all modes to
defaults
              esc'[0m'   Resets graphics modes to defaults

Text Styles   esc'[1m'   Boldface on
              esc'[3m'   Italics on
              esc'[4m'   Underline on
              esc'[7m'   Reverse Video on
              esc'[0m'   Reset to defaults

Text Colours  esc'[30m'  Text Colour 0
              esc'[31m'  Text Colour 1
              esc'[32m'  Text Colour 2
              esc'[33m'  Text Colour 3
              esc'[3#m'  Text Colour # = 4-8 if 8 colour screen
              esc'[0m'   Reset to defaults

Background    esc'[40m'  Background Colour 0
              esc'[41m'  Background Colour 1
              esc'[42m'  Background Colour 2
              esc'[43m'  Background Colour 3
              esc'[4#m'  Background Colour # = 4-8 if 8 colour
screen
              esc'[0m'   Reset to defaults

Note - Default workbench screen colours are:-

                       @{B}@{U}WB V 1.x@{UU}   @{U}WB V 2 & 3@{UB}@{UU}
             Colour 0   Blue       Grey
             Colour 1   White      Black
             Colour 2   Black      White
             Colour 3   Orange     Blue

Other items   esc'[#u'   Set maximum length of lines in window to #
              esc'[#t'   Set maximum number lines in window to #
              esc'[#x'   Start text # pixels from left of window
              esc'[#y'   Start text # pixels from top of window


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "Putting" "Article 15 - More about Strings - Putting Escape Codes Into SAY Instructions"

@{B}@{U}@{JCENTER}Putting Codes Into SAY Instructions@{UB}@{UU}
@{JLEFT}
When using escape sequences, the special codes that come after "esc" MUST
be put into quotes and there must NOT be a space between the "esc" code
and the special codes. For example:-

  esc = '1b'x
  SAY esc '[1mThis is in BOLD'

will display:-

  [1mThis is in BOLD

without the text being in bold, but:-

  esc = '1b'x
  SAY esc'[1mThis is in BOLD'

will display the text "This is in BOLD" in bold type.

The escape sequences can be put at any point of the text in a SAY
instruction. More than one escape sequence can be used provided that each
one starts with the "esc" code.

As an example, try out this program (although it is small, I have put
it on disk as I cannot demonstrate the colour changes in this
text!):-

  /* Example15-2 */

  esc = '1b'x
  SAY esc'[1m'esc'[33mThis is bold colour 3 'esc'[4mand
      underlined'esc'[0m'

If you want to have the special effects for the one line only, then include
the:-

   esc'[0m'

shown at the end of the above line to turn it all off.

The best thing to do is to experiment with them until you find the effect
that you want.

@{"Example15-3" LINK "ARB:Articles_11-20/Example15-3.rexx/MAIN"} shows how you can assign the escape codes to symbol names
to make things easier for you. If is a bit big to include here so double
click on it to read it. You should be able to follow it through without
explanation.


@{JCENTER}=== End of Text ===
@{JLEFT}















@ENDNODE
