Unit 2 · Lesson 6 Beginner ⏱ ~40 minutes

Variables and Data Types

You've been using variables since Lesson 3. Now it's time to go deeper — understanding what kinds of data HyperXTalk works with, how to manipulate text and numbers, and how to build and process lists. These are the building blocks of almost every real application you'll write.

🎯 Learning Objectives
  • Understand how HyperXTalk handles different types of data
  • Manipulate text using string functions
  • Perform arithmetic and use mathematical functions
  • Build, read, and modify comma-separated lists
  • Convert between text and numbers

📦 Data Types in HyperXTalk

Unlike many programming languages, HyperXTalk doesn't have strict data types. Every value is stored as text under the hood — but HyperXTalk is smart enough to treat it as a number when you do arithmetic, as a date when you compare dates, and as a list when you use item functions.

This means you rarely need to worry about converting between types — HyperXTalk figures it out:

put "42" into tValue -- stored as text "42" add 8 to tValue -- treated as number → tValue is now 50 put tValue & " items" -- treated as text → "50 items"
💡 The special value empty represents a variable or field that contains nothing at all — equivalent to an empty string "". Use if tVariable is empty then to check for it.

📝 Working with Text

HyperXTalk has a rich set of tools for working with text. You can measure it, slice it, search it, and transform it — all in plain English:

on mouseUp local tString, tLen, tWord, tChars put "Hello, HyperXTalk!" into tString put the length of tString into tLen put word 2 of tString into tWord put char 1 to 5 of tString into tChars answer tLen & ", " & tWord & ", " & tChars end mouseUp
Script editor showing string manipulation
String manipulation — length, word extraction, and character slicing — click to enlarge

Common Text Operations

-- Measure text put the length of tString -- number of characters put the number of words in tString -- number of words put the number of lines in tString -- number of lines -- Extract parts put word 1 of tString -- first word put word 2 to 4 of tString -- words 2, 3 and 4 put char 1 to 5 of tString -- first 5 characters put last word of tString -- last word -- Search if "XTalk" is in tString then ... -- contains check put offset("XTalk", tString) -- position of substring -- Modify put toUpper(tString) -- ALL CAPS put toLower(tString) -- all lowercase replace "Hello" with "Hi" in tString -- find and replace
🟢 HyperXTalk text comparisons are case-insensitive by default — "hello" = "Hello" is true. If you need case-sensitive comparison, use if tA is tB with set the caseSensitive to true first.

🔢 Working with Numbers

HyperXTalk supports all standard arithmetic operators and a useful set of mathematical functions:

on mouseUp local tNum, tDouble, tRoot, tRounded put 42 into tNum put tNum * 2 into tDouble put sqrt(tNum) into tRoot put round(tRoot) into tRounded answer tDouble & ", " & tRoot & ", " & tRounded end mouseUp
Script editor showing number operations
Number operations — arithmetic, square root, and rounding — click to enlarge

Arithmetic Operators

+ addition · - subtraction · * multiplication · / division · ^ power · mod remainder

Shorthand Arithmetic

add 1 to tCount subtract 5 from tScore multiply tPrice by 1.1 divide tTotal by tCount

Useful Math Functions

round(tNum) -- round to nearest integer trunc(tNum) -- remove decimal part abs(tNum) -- absolute value sqrt(tNum) -- square root max(tA, tB) -- larger of two values min(tA, tB) -- smaller of two values random(tNum) -- random integer from 1 to tNum

📋 Working with Lists

In HyperXTalk, a list is simply a text value with items separated by commas. There's no special list type — any variable can become a list just by putting comma-separated values into it:

on mouseUp local tList, tSecond, tCount put "apple,banana,cherry" into tList put item 2 of tList into tSecond put the number of items in tList into tCount put "mango" into item 4 of tList answer tSecond & ", " & tCount & ", " & tList end mouseUp
Script editor showing list operations
List operations — reading, counting, and adding items — click to enlarge

Common List Operations

-- Read items put item 1 of tList -- first item put last item of tList -- last item put the number of items in tList -- how many items -- Modify items put "mango" into item 4 of tList -- set or add item 4 delete item 2 of tList -- remove item 2 -- Build a list with a loop repeat with tI = 1 to 5 put tI & "," after tList end repeat
🟢 Lists are also the key to working with multi-line data. Change the item delimiter from a comma to a return character and item functions will work on lines instead: set the itemDelimiter to return.

🔄 Type Conversion

Because HyperXTalk handles types automatically, conversion is usually just a matter of using the right function or operation:

-- Text to number put "3.14" into tText multiply tText by 2 -- tText is now 6.28 (treated as number) -- Number to text put 42 into tNum put "The answer is " & tNum -- joined as text automatically -- Check if a value is a number if tValue is a number then -- safe to do arithmetic end if
🛠️ Exercise — A Shopping List Builder

In this exercise you'll build a simple shopping list app that lets users add items, shows a count, and calculates a summary. Click any thumbnail to enlarge it.

  1. Create a new stack with a Field named ItemInput for typing new items, a Button labelled Add Item, a scrolling Field named ShoppingList with Lock text enabled, and a Field named Summary with Lock text enabled.
  2. Edit the script of the Add Item button:
    button "Add Item"
    on mouseUp put the text of field "ItemInput" into tItem if tItem is empty then answer "Please type an item first." exit mouseUp end if -- Add item to the list field put tItem & return after field "ShoppingList" -- Update summary put the number of lines of field "ShoppingList" \ into tCount set the text of field "Summary" \ to tCount & " item(s) on your list." -- Clear the input set the text of field "ItemInput" to empty end mouseUp
    String operations
    String operations
    List operations
    List operations
  3. Switch to Browse mode and add several items. The Shopping List field should grow with each addition and the Summary should update the count.

Bonus challenge: Add a Clear List button that sets both fields to empty and resets the summary to "0 item(s) on your list."

📝 Review Questions
Question 1
Does HyperXTalk require you to declare a variable's type before using it?
No — HyperXTalk stores all values as text internally and automatically treats them as numbers, dates, or lists depending on the context. You can just assign a value and use it without any type declaration.
Question 2
How do you extract the first 5 characters of a string stored in tString?
put char 1 to 5 of tString into tResult — or just char 1 to 5 of tString anywhere you need the value.
Question 3
What function returns a random integer between 1 and a given number?
random(n) — e.g. put random(6) into tDice simulates a six-sided die roll, returning a number from 1 to 6.
Question 4
What separates items in a HyperXTalk list by default?
Commas — e.g. "apple,banana,cherry" is a three-item list. You can change this with set the itemDelimiter to return to work with line-separated data instead.
Question 5
How do you check if a variable contains a number rather than text?
if tValue is a number then — this checks whether the value can be treated as a number. Useful for validating user input before doing arithmetic.