Transcript Slide 1

How to...

Do things with text

Copyright (C) Previca Limited 2013 All rights reserved. v1.1

H e l l o w o r l d

How to...

Do things with text

This “How to” guide shows you how to do things with text. In lots of apps we need to understand how long some text is, extract elements of the text one by one, look for patterns in the text, convert between text and ASCII codes, change text between upper and lower case, and so on. This handy guide shows you how to do that.

To keep this guide simple, text is entered using the KEY.READLINECASE() function. To read text from files please see the “How to... Do things with files” guide.

• • • • •

Background:

YOUSRC (pronounced “you source”) is a

free online learn-to-program environment

that has been used in schools since 2010. To date, 500+ teachers and 15,000+ students have registered to use the resource.

YOUSRC is most commonly used in secondary schools, either in Year 9 to give all students a fun taste of programming, or in Years 10 and 11 as a core part of the new GCSE qualifications in Computing. Teachers can request

free

Teacher Privileges for the site, which gives them access to a Teacher Admin Portal. Here they can create student accounts, monitor how their students code, and even run their apps.

Everything you need to write apps is available on the site for free, paid for by advertising and sponsorship.

Nothing needs to be installed

on your school computers except the free Java Runtime Environment (which most schools already have for things like Scratch) - we use this to check and run your apps.

Visit the YOUSRC site at www.yousrc.com

Copyright © Previca Limited, 2013. Schools may freely use these resources.

2

Contents:

1.

Some YOUSRC essentials – if you are diving straight in 2.

3.

4.

5.

6.

7.

8.

9.

What is text, and how is it stored in your computer?

ASCII codes Entering text, and displaying it on the screen Comparing two texts for a match Adding text together Converting text to upper or lower case Checking to see if some text is actually a number Converting a number to text 10. Converting text to a number 11. Finding the length of some text 12. Extracting each element of text, one by one 13. Finding the first position of some text in some other text 14. Finding all positions of some text in some other text 15.

Extracting text between comma’s 16. Splitting a text list into an array of text items • Appendices Appendix A: YOUSRC overview for teachers Copyright © Previca Limited, 2013. Schools may freely use these resources.

How to...

Do things with text

3

How to...

Do things with text

1. Some YOUSRC essentials – if you are diving straight in

If you have already done some ELC programming at the YOUSRC website then you can skip this bit, but for people new to ELC / YOUSRC these notes are important!

1.

2.

3.

4.

5.

6.

7.

8.

YOUSRC uses a very easy to learn language called ELC, which is based on the “best bits” from other languages such as Basic, C, and BCPL. ELC is a proven stepping stone to other more complex languages.

ELC supports global and local variables which can be numbers or text (append $), arrays, function calling with parameters, for loops, while loops, if statements – everything you need to write true programs.

An overriding design principle of ELC is that only one thing is done per line of code

. This is a key element that makes it a proven learning language as it is easy to ready by others (eg teachers), and it is easy to learn from other people’s code or from examples as the code is extremely readable.

To help reinforce what each line does the ELC language starts each line with a keyword, unless the line is a function call in which case the line simply comprises the function call with associated parameters.

To highlight how simple the language is – there are just 9 keywords and just 100 powerful library functions.

To avoid mixing assignment, conditional checks, or compound function calls in one line of code return values from library functions are through two global variables: RETVAL (numbers) and RETVAL$ (words).

This also means that functions can return two things at once if needed – a number in RETVAL and words in RETVAL$ - making library functions even more powerful. The online library reference has full details.

Finally, to minimise students finding coding problems when running apps each app is checked through an online checker first that specifically generates error messages tailored for students – very clear, making suggestions on how to fix a problem, and not aimed (like most languages) at experienced programmers.

Copyright © Previca Limited, 2013. Schools may freely use these resources.

4

How to...

Do things with text

2. What is text, and how is it stored in your computer?

1 2 3 4 5

H e l

72

l o

101 108 108 111

6

32

7 8 9 10 11

w o r l d

119 111 114 108 100 1.

2.

3.

4.

5.

6.

7.

Text is a sequence of items such as letters of the alphabet, number digits, or special things like # or % In programming, a sequence like this is often called a “

string

” and each item is often called a “

character

” Whereas a number is stored in a single memory location in your computer, a text string needs a series of memory locations one after the other, as each character is stored by a number called an ASCII code The ASCII (pronounced “askee”) codes were created in the 1960’s, and provide a way of all computers, programs, and devices like printers to represent data and communicate text strings in the same way – ASCII is the abbreviation of the American Standard Code for Information Interchange The next page of this guide provides a table mapping ASCII codes to different characters There are some special characters, and these have values below 32 or above 126 – These are either not printable, or are used for special characters, or alphabet letters from other languages From this you can see the following about the text string “Hello world” – – It is 11 characters long, so needs 11 memory locations to store the whole text string It is stored using the ASCII codes 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 Copyright © Previca Limited, 2013. Schools may freely use these resources.

5

3. ASCII codes

How to...

Do things with text

Copyright © Previca Limited, 2013. Schools may freely use these resources.

6

How to...

Do things with text

4. Entering text, and displaying it on the screen

• • • • • • • • The code above is as it appears in the online editor, where green confirms correct spelling of keywords (such as

GLOBAL

) and library function names (such as

GFX.WRITE

), blue is text inside of quote marks that will be displayed on screen, grey text lines are comments (lines starting

//

), and black is all other code.

Create a global variable called

Text$

, where the

$

means it will store text not a number

(line 3)

Display an instruction to user

(line 8)

Let the user type in a line of text allowing upper or lower case letters, storing result in

RETVAL$

Store the text that the user entered in the variable called

Text$

(line 10)

Note that

GFX.NEWLINE()

starts a new line

(line 9)

Tell the user what text they entered

(lines 14-15)

You could have used the function

KEY.READLINE()

which just reads in upper case text if you prefer Copyright © Previca Limited, 2013. Schools may freely use these resources.

7

5. Comparing two texts for a match

How to...

Do things with text

• • • • • Create a global variable called

Text$

, as we have done before

(line 3)

Read in and store some text, as we have before

(lines 8-10)

Check to see if the entered text matches the set password

(line 14)

If the entered text matches then display the correct password message

(line 16)

If the entered text does not match then display the incorrect password message

(line 20)

Copyright © Previca Limited, 2013. Schools may freely use these resources.

8

6. Adding text together

How to...

Do things with text

• • • • • Create three global variable called

Text1$

,

Text2$

, and

Text3$

Read in and store some text into

Text1$

, as we have before

(line 3) (lines 8-11)

Read in and store some text into

Text2$

, as we have before

(lines 14-17)

Create some new text which is “The cat ”, plus

Text1$

, plus a space, plus

Text2$

(line 20)

Display this new text message, which is all these bits of text added together

(line 21)

Copyright © Previca Limited, 2013. Schools may freely use these resources.

9

7. Converting text to upper or lower case

How to...

Do things with text

TEXT.TOUPPER()

, and

TEXT.TOLOWER()

Copyright © Previca Limited, 2013. Schools may freely use these resources.

do exactly what you expect – change case

(lines 18 & 26)

10

How to...

Do things with text

8. Checking to see if some text is actually a number

• • • •

TEXT.ISNUM()

sets

RETVAL

to

TRUE

or

FALSE

to show if the text was actually a number or not

TRUE

is a number with value 1, and

FALSE

is a number with value 0

(line 18)

This means you could check if

RETVAL

has value 1, but using

TRUE

makes the code more readable Note that

GFX.WRITE()

displays some text, and

GFX.WRITELINE()

displays text and moves to new line Copyright © Previca Limited, 2013. Schools may freely use these resources.

11

9. Converting a number to text

How to...

Do things with text

• •

UTIL.RANDOM()

sets

RETVAL

to a random number between 0 and the specified number

TEXT.FROMNUM()

sets

RETVAL$

to the text representation of the specific number

(line 8) (line 12)

Copyright © Previca Limited, 2013. Schools may freely use these resources.

12

10. Converting text to a number

How to...

Do things with text

• •

TEXT.TONUM()

sets

RETVAL

to the number value of the numeric text

(line 22)

We could avoid the

TEXT.TONUM()

altogether, if we wanted, because both

KEY.READLINE() KEY.READLINECASE()

set

RETVAL

and to the numeric value of entered text if the entered text is a number Copyright © Previca Limited, 2013. Schools may freely use these resources.

13

11. Finding the length of some text

How to...

Do things with text

TEXT.LENGTH()

sets

RETVAL

to the length of the text

(line 14)

Copyright © Previca Limited, 2013. Schools may freely use these resources.

14

How to...

Do things with text

12. Extracting each element of text, one by one

• • As we said earlier, each element of a text “string” is called a “character”

TEXT.GETPART()

takes three parameters: The original text; the character number to start extraction; and the number of characters to extract. Here we pass the text that was entered by the user, and a number variable called

Index

which loops between the values

1

and the length of the text, Each time around the loop we extract

1

character and store it in the

Char$

variable before displaying it

(lines 18-25)

Copyright © Previca Limited, 2013. Schools may freely use these resources.

15

How to...

Do things with text

13. Finding the first position of some text in some other text

TEXT.FIND()

takes three parameters: The text to search in; the text to search for; and the start position to start searching – here we use 1. It sets

RETVAL

to the position where the search text is found, or -1 if the search text is not found

(line 20)

Copyright © Previca Limited, 2013. Schools may freely use these resources.

16

How to...

Do things with text

14. Finding all positions of some text in some other text

• We use the

Position

variable to control where we start searching, moving beyond the current position if a match is found, and ending the

WHILE

loop when no more matches are found Copyright © Previca Limited, 2013. Schools may freely use these resources.

17

15. Extracting text between comma’s

How to...

Do things with text

• • The

TEXT.GETCSV()

sets

RETVAL$

to the data that is between commas given some text and the data element number. If no more data is available then

RETVAL$

is set to empty text string “”.

The loop above continues until there is no more data and the empty string is detected.

Copyright © Previca Limited, 2013. Schools may freely use these resources.

18

How to...

Do things with text

16. Splitting a text list into an array of text items

TEXT.SPLIT() RETVAL$

has two parameters: The text to split, and the string that is the text separator. It sets to be an array of split bits of text, and

RETVAL

to be the number of items the text was split into.

Copyright © Previca Limited, 2013. Schools may freely use these resources.

19

How to...

Do things with text

Appendices

Copyright © Previca Limited, 2013. Schools may freely use these resources.

How to...

Do things with text

Appendix A: YOUSRC overview for teachers

        

For teachers

Free lesson plans and lesson plan videos Online web-based environment No software installed on school computers Ideal for short taster courses for all of Year 9, and used very successfully for GCSE Computing Good for computer clubs and HE/FE enrichment Fun spy-themed code missions – marked for you!

Private help for teachers in online Help Forum Regular webinars and on-line support sessions Access to Teacher Admin portal:

Create student accounts

Review student activity

Run student apps

Review their code

See history of code changes

Lock controlled assessments

For students

             

Easy to learn ELC language Stepping stone to C or Java Special “student friendly” code checker Use YOUSRC at school or at home Quick and highly-visual results Apps run in web browsers, and unchanged on Android devices & Raspberry Pi Child-safe, family-friendly policies throughout Publish apps to Showcase to show friends Learn from other peoples code Comment on other peoples apps Create your own versions of other people’s apps Help forum to get assistance Sell access to your apps on YOUSRC App Shop – great for school enterprise projects Create Android apps to sell on Google Play Store See the teacher resources at www.yousrc.com > Teacher info > Resources

Copyright © Previca Limited, 2013. Schools may freely use these resources.

21