3.2 · Arrays, strings & advanced types

Level 3 · Advanced: in-depth WLangage

3.2Arrays, strings & advanced types

Goal: Choose the right structured type and control conversions for reliable data processing.
Estimated time: 11 min · self-paced reading · If you only have 5 min: read “The essentials in 30s” + the diagram.

The essentials in 30 seconds

  • The indexed array suits ordered lists; the associative array retrieves a value by key without traversal, ideal for mappings and caches.
  • The structure groups related data under a named type; strings are split, cleaned and compared with caution about accents.
  • Any text → number or date conversion must be controlled: separators, date formats and empty values are classic traps.

Running mini-case — Yacine imports the supplier catalogue

Yacine must import a supplier's catalogue of electrical equipment: a CSV file of twenty thousand lines with item code, label, price and family. He starts by splitting each line on the semicolon, then checks the number of columns. To find a family's label without traversing the whole file, he loads the mappings once into an associative array. He defines a structure AdresseLivraison to group street, postal code and city, rather than carrying five variables around. Finally, he replaces the decimal comma with a dot and checks that the price is numeric before converting it. He notes the reflex: right type, direct key, controlled conversion.

Self-contained case for this section: it returns in the course, diagram, script and 48h challenge of section 3.2.

1. Indexed and associative arrays

An array is an ordered collection of elements of the same type, accessible by an index. It can be traversed with POUR TOUT, added to with TableauAjoute, sorted and searched. There are dynamic arrays, whose size varies, and arrays with several dimensions. The associative array files each value under a key — often a string, for example an item code. Access is direct, without traversing the whole collection: it is perfect for mappings and in-memory caches.

2. Structures and strings

A structure is a type you define to group related information under a single name: an Adresse structure contains street, postal code and city. It makes the code readable and replaces a handful of scattered variables. Strings occupy a central place in management: concatenate, extract, measure with Taille, search with Position, replace, change case, remove spaces and above all split on a separator to read a CSV or imported data.

3. Conversions and accents

Conversions between text, numeric and date are a classic source of errors: a user input or an imported file can contain anything. Before converting, Yacine checks that the value is not empty, that the decimal separator is the right one and that the date matches the expected format. Accents and special characters also require vigilance during comparisons, sorts and file exchanges, because the encoding can change the result.

Common misinterpretation: “An associative array is always slower than a classic array; a structure can contain methods; and converting a string to a number is no problem”. No, three mistakes. The associative array is on the contrary direct by key, whereas the classic array requires a traversal. A structure groups only data: it is classes that carry methods and encapsulation. And a conversion assumes a valid format: without checking, a decimal comma or an ambiguous date produces a wrong value, sometimes without any visible error. Yacine always checks before converting.
Importing a catalogue without errors Split, check, convert, then index by key. Split the CSV lineCheck columns andformatsConvert pricesand datesIndex item key → label
Split, check, convert, then index by key.
Text version of the diagram (SEO / screen readers / translation): 1) Yacine imports a supplier catalogue of twenty thousand lines for his application trading electrical equipment in Oran. 2) He splits each line on the semicolon with a string function, checks the number of columns, replaces the decimal comma with a dot and checks that the price is numeric before conversion. 3) He loads the family → label mappings into an associative array for direct access, and groups the address information in a structure AdresseLivraison. Lesson: choosing the right type and checking every conversion makes the import reliable and fast.

🔎 Concrete example — 🔎 Concrete example — Split, check, index

Yacine processes a line of the supplier catalogue before integrating it:

// CSV line: code;label;price;family
sLigne est une chaine = "EL042;Disjoncteur 32A;12,50;Protection"
sColonnes est un tableau de chaines = ChaineDecoupe(sLigne, ";")

SI TableauOccurrence(sColonnes) <> 4 ALORS
    Erreur("Ligne invalide : " + sLigne)
    RENVOIE Faux
FIN

sPrix est une chaine = Remplace(sColonnes[3], ",", ".")
SI PAS EstNumerique(sPrix) ALORS
    Erreur("Prix non numerique pour " + sColonnes[1])
    RENVOIE Faux
FIN

nPrixHT est un monetaire = sPrix
tFamilles["Protection"] = "Appareillage modulaire"

Reading: the split isolates the fields, the check blocks doubtful lines.

Lesson: the “Protection” key gives direct access to the label, without traversal.

Section vocabulary

Dynamic array
Ordered collection of elements of the same type, whose size can change at runtime.

Simple example: Yacine adds each valid catalogue line with TableauAjoute.

What it is not: a structure that groups named fields.

Reflex: variable ordered list → dynamic array.

Associative array
Collection where each value is filed under a key, with direct access without traversal.

Simple example: Yacine retrieves a family's label by its code.

What it is not: an indexed array that must be traversed to find a value.

Reflex: retrieve by key → associative array.

Structure
Type defined by the developer, grouping several related data under a single name.

Simple example: AdresseLivraison contains street, postal code and city.

What it is not: a class with methods and encapsulation.

Reflex: group data → a structure.

String splitting
Operation that separates a string into pieces on a separator, very useful for CSV.

Simple example: Yacine splits each catalogue line on the semicolon.

What it is not: a global comparison of the line without extracting the fields.

Reflex: read a CSV → split the string.

Controlled conversion
Transformation of one type into another after checking the format and the edge cases.

Simple example: Yacine checks the price before converting it to monetary.

What it is not: converting an input directly without checking that it is numeric.

Reflex: change type → check first.

In practice — In practice — Manipulating arrays, structures and strings

  1. Create a dynamic array, feed it with TableauAjoute and traverse it with POUR TOUT, like Yacine.
  2. Use an associative array for a key → value mapping (item code → label): the search is direct.
  3. Define a structure AdresseLivraison to replace several separate variables in the order screens.
  4. Split a CSV line on the separator, then check and convert each value before using it.
You handle sets of data in memory and strings cleanly, with the reflex of checking every conversion.

Step-by-step script — import a catalogue like Yacine (4 min)

1) Open the CSV file and spot the separator as well as the format of dates and prices. 2) Split each line and check the number of columns. 3) Check the values, replace the decimal separator if needed, then convert. 4) Load the mappings into an associative array to avoid repeated traversals. 5) Group the related information in a structure and log the rejected lines.

Keep this reflex: a silent import that accepts everything produces wrong data, not a time saving.

Copy-paste template — Yacine's advanced types sheet

Array          : ordered list, index, TableauAjoute, POUR TOUT
Associative    : key -> value, direct access (mappings, caches)
Structure      : related data under one name (AdresseLivraison)
String         : Taille, Position, Remplace, split on separator
Conversion     : check empty, format, separator BEFORE converting

A price with a comma

Yacine replaces the comma with a dot and checks the value before conversion.

A repeated search

Yacine indexes the families in an associative array: no more traversal on each line.

A scattered address

Yacine groups street, postal code and city in a structure AdresseLivraison.

An ambiguous date

Yacine imposes the expected format and rejects unreadable dates instead of guessing them.

✓ Best practices

  • Choose the type according to the access wanted (index or key).
  • Split strings rather than compare the whole line.
  • Check before any conversion of input or import.

✗ Common pitfalls

  • Traversing a large array to retrieve by key.
  • Converting an empty or badly separated string.
  • Neglecting accents and file encoding.

Key takeaways

  • Indexed array for order, associative array for direct access by key.
  • The structure groups related data; it carries no methods.
  • Strings are split, cleaned and compared with vigilance.
  • Any text → number or date conversion must be checked.

48h challenge — apply it tomorrow

Like Yacine, take a real CSV file from your application and split its lines on the separator. Load a key → value mapping into an associative array, then check and convert the prices and dates before using them. Finally group a complete address in a structure and note in one sentence the lines you had to reject — and why.

Section exit checklist
  • I choose between an indexed array and an associative array according to access.
  • I know how to define and use a structure of related data.
  • I check conversions and encodings before use.
Self-assessment 1-5: do I choose the right type without hesitating?Self-assessment 1-5: do I check before converting?

Frequently asked questions

When should you use an associative array rather than a classic array?

When you must retrieve a value by a key — an item code, a customer code, an identifier. Access is direct, whereas a classic array requires traversing the elements. Over ten elements the difference is invisible; over twenty thousand repeated import lines, it becomes decisive. The classic array remains preferable when order matters or when the whole collection is systematically traversed.

Why do type conversions cause so many bugs?

Because a conversion assumes that the data has the expected format, which is rarely guaranteed with an input or an external file. Comma or decimal point, date in day/month or month/day format, thousands separators, empty value: so many cases that produce zero or a wrong value without any message. The rule is simple: check before converting, and specify the expected format.

Check your understanding

Which type should you choose to retrieve a label from an item code?

Additional resources (optional, to go further)

Level 3 examFresh, harder questions to test what you've learned