Recap: Arrays and Maps
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 22 of 80.
Challenge
MediumCount each fruit and print the counts in alphabetical order.
- Loop over
itemswithipairs, counting intolocal counts: {[string]: number} = {}. - Collect the keys into
local names: {string} = {}and calltable.sort(names). - For each name in
names, print`{name}: {counts[name]}`.
Press Run Code: the output shows apple: 3, banana: 2 and cherry: 1.
Try it yourself
local items: {string} = {"apple", "banana", "apple", "cherry", "banana", "apple"}
-- 1) count each item into counts: {[string]: number}
-- 2) collect the keys into names: {string} and sort them
-- 3) print `{name}: {count}` for each sorted name
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Number, String and BooleanThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler