Teacup #3: Arrays & Real-World Examples

Arrays & Real-World Examples


From single values to collections

So far, we’ve worked with single pieces of data:

  • One name

  • One number

  • One decision

But real life is rarely that simple.

We don’t have one friend — we have many.
We don’t store one task — we manage lists.
We don’t process one item — we process collections.

This is where arrays come in.


1. Arrays – Keeping many things together

An array is simply a list of values stored under one name.

In GTLang, arrays are written using square brackets [].

A simple array

$fruits : ["Apple", "Banana", "Orange"]

Read it like this:

“Fruits is a list containing Apple, Banana, and Orange.”

That’s it.

No special keywords.
No strict types.
Just data.


Accessing array elements

Arrays start counting from 0.

@echo $fruits[0]
@echo $fruits[1]

Output:

Apple
Banana

Modifying an array

$fruits[1] : "Mango"
@echo $fruits[1]

Output:

Mango

You can change data anytime.
Nothing is locked.


Adding new elements

$fruits[] = "Grape"

Now the list grows naturally — just like real life.


2. Looping through arrays (the natural way)

Arrays become powerful when combined with loops.

Printing all items

foreach $fruits
@echo $_value

Output:

Apple
Mango
Orange
Grape

Here:

  • $_value means “the current item”

  • You don’t need indexes unless you want them


With index and value

foreach $fruits as $index $fruit
@echo $index ". " $fruit

Output:

0. Apple
1. Mango
2. Orange
3. Grape

Clean. Predictable. Easy to read.


3. Real example #1 – A simple todo list

Let’s build something useful.

Step 1: Create tasks

$tasks : ["Learn GTLang", "Write code", "Drink green tea"]

Step 2: Show tasks

@echo "Today's tasks:"
foreach $tasks as $i $task
@echo ($i + 1) ". " $task

Output:

Today's tasks:
1. Learn GTLang
2. Write code
3. Drink green tea

This is already a real program.


4. Arrays with mixed data (because life is mixed)

GTLang does not force everything to be the same type.

$user : ["John", 24, true]

This array contains:

  • A string

  • A number

  • A boolean

That flexibility makes GTLang very beginner-friendly

GREENTEA-PROGRAMMING-LANGUAGE-M…


5. Real example #2 – Finding data in a list

Let’s search for a name.

$names : ["Annie", "Bob", "John", "Sabrina"]

foreach $names as $i $name
if $name = “Bob”
@echo “Found Bob at position ” ($i + 1)

Output:

Found Bob at position 2

This is a classic beginner task, and GTLang makes it readable instead of scary.


6. Nested arrays – Data inside data

Sometimes, data has structure.

Example: students and their scores.

$students : [
["John", 8],
["Annie", 9],
["Bob", 6]
]

Loop through them:

foreach $students as $student
@echo $student[0] " scored " $student[1]

Output:

John scored 8
Annie scored 9
Bob scored 6

You are now working with structured data.


7. Real example #3 – Simple grading system

$students : [
["John", 85],
["Annie", 92],
["Bob", 60]
]
foreach $students as $student
$name : $student[0]
$score : $student[1]if $score >= 90
$grade : “Excellent”
elif $score >= 70
$grade : “Good”
else
$grade : “Needs improvement”

@echo $name ” → ” $grade

Output:

JohnGood
AnnieExcellent
BobNeeds improvement

This is real logic, not just syntax practice.


8. Why arrays matter

Arrays allow you to:

  • Handle multiple values

  • Build real programs

  • Represent real-world data

  • Think in structures, not lines

Once you understand arrays, programming stops feeling like typing —
it starts feeling like designing ideas.


Final thoughts – You’re building real programs now

At this point, you can:

  • Store data

  • Group data

  • Loop through data

  • Make decisions

  • Build small systems

That is not “just learning”.

That is programming.

GTLang simply removes unnecessary barriers so you can focus on thinking, not syntax.

In the next part, you can explore:

  • Objects and simple classes

  • Working with files

  • Small automation scripts

  • Turning ideas into tools

Slowly. Calmly.
Like green tea ☕🌱