XML

kent logo

CO538 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for strings

2012

Question 35 (2012):

Submission reference: IN2189

Is there a way to join strings together easily? For example, in Java you can just use '+'. Thanks.

Answer 35:

Classical occam was definitely not designed for string handling, :(. Strings are BYTE arrays and arrays have fixed lengths, with those lengths fixed at compile time, :( :(. For further Q&As on string difficulties, follow the lilnks to "strings" and "string-handling" in the keyword index.

We did provide a string handling library but it's painful to use. A string has to be maintained in two variables: a BYTE array sufficiently long and an INT recording its actual length. (Aside: with occam-pi, this could be simplified to a single RECORD variable – see "Shared-etc" slides 94-116 – but it would still be a painful.) If an attempt is made to extend a string beyond the size allowed by the BYTE array, it just crashes! To try it out (not recommended), include:

    #INCLUDE "string.module"

in your program.

However, occam-pi has dynamic arrays whose size is set at run-time, which means they can also be changed – see "Mobiles" (which is not an examinable part of this course) slide 14. Further, occam-pi has user-defined operators and user-defined types, which means that a library can be set up defining a dynamic STRING type and a whole bunch of infix operators (e.g. "+" for joining them). This would be a useful mini-project (or part of a larger one).

Having said the above, why do you want to join strings together for this assessment (Dining Phils Animation)? The college processes should not be working with strings – see the last paragraph ("Do not attempt ...) of the answer to Question 31 (2012). Only the display process needs to deal with strings and, then, only to output them to the screen!. To "join" strings together for such output is unnecessary – simply output them in sequence.

Keywords: q7 , strings , string-handling

2011

Question 40 (2011):

Submission reference: IN2071

Since strings within a list have to be padded (see Question 104 (2003)), I'd like to keep track of the actual length of a string (sans padding).

For example:

    "Cherry Tree": 11
    "Cuban	": 5
    ...
    "Lady Luck	": 9

Now, I believe 3D arrays are one way to achieve this. 2D arrays blow my mind far enough, but 3D takes it to the next level of discombobulation!

This feels like it should work:

    VAL [][][]BYTE a IS [["Cherry Tree", [11]], ["Cuban      ", [5]]]:

... but woefully gives me:

    table elements are of different types

What am I missing? Can it be simplified?

Answer 40:

The issue here is that "[11]" is of type "[1]BYTE", rather than of type "[11]BYTE". The simplest method (in my view anyhow) is to use a RECORD type (which we've now covered in the lectures) along the lines of:

    VAL INT MAX.STRING.LEN IS 128:

    DATA TYPE MY.STRING
      RECORD
        [MAX.STRING.LEN]BYTE str:          --* string data.
        INT len:                           --* actual length of string.
    :

Then you can easily create an array of these things:

    [42]MY.STRING string.table:

You'll probably want some helper PROCs to manage and display these sorts of things, but that's trivial enough:

    PROC set.my.string (MY.STRING s, VAL []BYTE str)
      SEQ
        IF
          (SIZE str) > MAX.STRING.LEN
            s[len] := MAX.STRING.LEN
          TRUE
            s[len] := SIZE str
        [s[str] FOR s[len]] := [str FOR s[len]]
    :

    PROC out.my.string (VAL MY.STRING s, VAL INT padding, CHAN BYTE out!)
      out.string ([s[str] FOR s[len]], padding, out!)
    :

Both of the above bits of code use array slices which haven't been covered in the lectures yet, but their operation is largely straightforward :-) — feel free to read up on them; they're at the end of the Shared-etc. slides.

Keywords: q7 , strings , string-handling

Referrers: Question 42 (2011)

2007

Question 58 (2007):

Submission reference: IN1406

Hi, could you tell me if it is possible and if so how to: initiallise an array of strings with values in it ie something like this

  VAL [][]BYTE strs IS ["hello","fish","mango"]:

i can't work out how to do it.

Answer 58:

In a 2-dimensional array, all sub-arrays must have the same dimension. Here, your individual strings are of different sizes. See Question 33 (2006) and related questions.

Keywords: q7 , strings

2006

Question 39 (2006):

Submission reference: IN1003

Using something like this:

    PAR i = 0 FOR 5

Can I make it produce Strings like "Fork 1", "Fork 2" etc., with some sort of concatenation? Such as "Fork " + 1 in Java?

Answer 39:

No. What on earth might a PAR replicator have to do with that?

Strings are not a primitive type in occam-pi. There are byte arrays that we use for them, but there are no built-in (or library) operators – such as concatenation. The follow-on module, Stage 3 Co632, introduces mobile and dynamically sized and allocated arrays. These, rather than the fixed-sized arrays used in this module, are from what strings (and a string library) should be made.

Meanwhile, please be assured that there is no need for any string concatenation when animating the dining philosophers. Only your animation process should deal with strings – outputting them to a screen channel. To output the string "Fork 2", where 2 is the value of a variable (say i):

    SEQ
      out.string ("Fork ", 0, screen!)
      out.int (i, 0, screen!)

Keywords: strings


Question 33 (2006):

Submission reference: IN995

How do you create an array of strings? This is the closest I have got:

  VAL [][]BYTE name.phil IS ["Bob", "James", "Fred", "Jane", "Amy"]:

But it doesn't work, and theres not a whole lot of information online :( Any hints?

Answer 33:

See Question 104 (2003) in particular, and other questions relating to strings and string-handling. Lots of stuff on-line!

Keywords: strings , string-handling

Referrers: Question 58 (2007)

2003

Question 104 (2003):

I want to have an array of strings (which I have understood to be an array of bytes) but am unsure how to use this. Is it:

    [element_length][index_location]string_array:

And also how would I write into a specific location, e.g. put "hello world" into index `0' in the string array?

Answer 104:

You are along the right lines with your declaration. More correctly:

    [number.of.strings][string.length]BYTE string.array:

The important thing to remember (as was covered to some extent in `q6') is that occam does not have an explicit `string' type -- they're handled using arrays of BYTEs. Thus you need to cater for `string length' separately (or use `null'-padding on the strings).

The above declaration declares an array of arrays of BYTEs. In other words, an array of fixed-size strings. If all the strings are going to be constant, declare it as a VAL array, e.g.:

    VAL [][]BYTE string.array IS ["hello world   ",
                                  "from occam    ",
                                  "but not from C"]:

Note that all the `strings' are the same-length -- the compiler will reject the declaration if this is not the case.

If you go down the `variable' route, data can be placed in the array using standard assignment, but the types must match exactly -- `[4]BYTE' and `[6]BYTE' are different types, for example. Thus assignments will often end up using array slices. See Question 44 (2002) for a comprehensive description of these. E.g.:

    [10][20]BYTE string.array:
    SEQ
      [string.array[0] FOR 11] := "hello world"          -- type is [11]BYTE
      [string.array[0] FROM 11] := "         "           -- pad remaining BYTEs with spaces

or this could be done as a single assignment:

      string.array[0] := "hello world         "          -- type is [20]BYTE

Keywords: arrays , strings

Referrers: Question 40 (2011) , Question 33 (2006)

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:33 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.