0

Why am I getting this syntax error in Zig?

parse_integers.zig:21:18: error: expected ')', found ','
    for (expected, list.items) |exp, actual| {

Here is the code (copied and pasted from the Zig home page):

const std = @import("std");
const parseInt = std.fmt.parseInt;

test "parse integers" {
    const input = "123 67 89,99";
    const ally = std.testing.allocator;

    var list = std.ArrayList(u32).init(ally);
    // Ensure the list is freed at scope exit.
    // Try commenting out this line!
    defer list.deinit();

    var it = std.mem.tokenize(u8, input, " ,");
    while (it.next()) |num| {
        const n = try parseInt(u32, num, 10);
        try list.append(n);
    }

    const expected = [_]u32{ 123, 67, 89, 99 };

    for (expected, list.items) |exp, actual| {
        try std.testing.expectEqual(exp, actual);
    }
}

I have Zig 0.10.1 installed.

I got this error by running:

zig test parse_integers.zig
8n8
  • 1,233
  • 1
  • 8
  • 21
  • I'm confused but I dont use zig, but you have this: const input = "123 67 89,99"; is this suppose to be a string that can be an array? cause you only have 1 comma, or is this number 89.99 and you want to magically make a decimal into an integer and the spaces are the separator between numbers? – easleyfixed Jun 22 '23 at 20:18
  • 1
    I think it's just a string. – 8n8 Jun 22 '23 at 20:28
  • Well the thing is called "parse integers" and you have a string, so it doesn't work. What happens if you put a comma in between the numbers like 1,2,3,4 ? Then if it is designed to loop through the string like an array, it will work if it knows to seperate the numbers on the "," – easleyfixed Jun 22 '23 at 20:29
  • Again forgive me i dont know zig but this part: , input, " ,"); It seems to be looking to seperate the input by using ", " which is a number comma and a space is required in between each number. Can you try to change input like this "123, 67. 89, 99"; (note a space after each comma) – easleyfixed Jun 22 '23 at 20:32
  • 1
    I don't know Zig either. But looking at the docs here: https://ziglang.org/documentation/master/std/#A;std:mem for `tokenize`, I think that the `" ,"` is an array of delimeters. So I think in this case `tokenize` splits up the string into a list of items separated by either space or comma. – 8n8 Jun 22 '23 at 20:38
  • I see, also nice that Ali figured it out, there is a direct fix for this sort of thing . – easleyfixed Jun 22 '23 at 20:46

1 Answers1

2

multi-range for loop has introduced in 0.11 (current master)

Ali Chraghi
  • 613
  • 6
  • 16