Lesson contents

Read a list by position

Read the value in the position you need. The example picks the second number.

Simple example

Start4 7 9
Result7
lesson.zig
const std = @import("std");

fn show(value: i64) !void {
    var buffer: [64]u8 = undefined;
    var writer = std.fs.File.stdout().writer(&buffer);
    try writer.interface.print("{d}\n", .{value});
    try writer.interface.flush();
}

pub fn main() !void {
    const values = [_]i64{4, 7, 9};
    try show(values[1]);
}
Output
7
Advertisements

Everyday examples

The last three readings were 18, 21 and 19 degrees. Show the middle reading.

Start18 21 19
Result21
lesson.zig
const std = @import("std");

fn show(value: i64) !void {
    var buffer: [64]u8 = undefined;
    var writer = std.fs.File.stdout().writer(&buffer);
    try writer.interface.print("{d}\n", .{value});
    try writer.interface.flush();
}

pub fn main() !void {
    const readings = [_]i64{18, 21, 19};
    try show(readings[1]);
}
Output
21
Run the example
Terminal
zig run lesson.zig
Official documentation