Multidimensional slices from strings

Hi,

got some problems to setup/initialize multidimensional slices from strings.

This works:
const EnigmaD = struct {
etw: []const u8 = “QWERTZUIOASDFGHJKPYXCVBNML”
};

What I want is a twodimensional slice. Kind of this:
const EnigmaD = struct {
etw: [][]const u8 = “QWERTZUIOASDFGHJKPYXCVBNML”, “WE”
};

…this is WRONG code…I know.
How do I have to change the code, that it compiles cleanly?

Cheers!
Tuxic

Maybe you want an array of slices?..

I want to setup/initialize a multidimensional slice from strings.

Not sure if you want exactly this, but here is an array of slices:

onst std = @import("std");
const dbg = std.debug;

pub fn main() void {
    const StringSlice = []const u8;
    var array_of_slices = [_]StringSlice {
        "123",
        "ABC",
        "ZYX",
    };

    for (array_of_slices) |slice| {
        dbg.print("{s}\n", .{slice});
    }

    for (array_of_slices) |slice| {
        for (slice) |letter| {
            dbg.print("'{c}',", .{letter});
        }
        dbg.print("\n", .{});
    }
}

There is no such thing, I believe.
There are multidimensional arrays, but not multidimensional slices.
There may be slices of multidimensional arrays.

I’m afraid that just an array of chars is not enough for describing a rotor of Enigma cipher machine, because

By itself, a rotor performs only a very simple type of encryption, a simple substitution cipher. For example, the pin corresponding to the letter E might be wired to the contact for letter T on the opposite face, and so on.

So you need some mapping from right side contacts to left side contacts.

Quick improvisation:

const EnigmaDevice = struct {

    const Letter = struct {
        char: u8,
        plate: u5, // left side contact
        pin: u5, // right side contact
    };

    const Rotor = struct {
        letters: [26]Letter,
        position: u5,
    }
    ...
};

:slight_smile:

There totally are! But I think their problem here is that they’re trying to initialize a mutable slice of non-mutable string slices from what resembles an array literal (i.e. anonymous) which is implicitly non-mutable.

For example, this cannot work:

    const EnigmaD = struct {
        etw: [][]const u8 = &.{ "QWERTZUIOASDFGHJKPYXCVBNML", "WE" },
    };

while this will work just fine:

    const EnigmaD = struct {
        etw: []const []const u8 = &.{ "QWERTZUIOASDFGHJKPYXCVBNML", "WE" },
    };

But, as you remarked, this might not be enough to tackle their problem anyway. If you want them mutable, make it an array of string slices, but at that point you need to know the size beforehand (or pass it in as a generic type parameter or similar).

my code ex:


// testing
  const allocator = std.heap.page_allocator;
  const Arg = struct {
    buf : std.ArrayList([] const u8) = std.ArrayList([] const u8).init(allocator),

   };


  var MlistArg = std.MultiArrayList(Arg){};
  defer MlistArg.deinit(allocator);

  try MlistArg.ensureTotalCapacity(allocator, 1);


  var xlist = std.ArrayList([] const u8).init(allocator);
  defer xlist.deinit();

  try xlist.append("France");
  try xlist.append("Paris");

  MlistArg.appendAssumeCapacity(.{
        .buf = xlist
    });



  
  xlist = std.ArrayList([] const u8).init(allocator);

  try xlist.append("Espagne");
  try xlist.append("Madrid");

  MlistArg.appendAssumeCapacity(.{
        .buf = xlist
    });

  xlist = std.ArrayList([] const u8).init(allocator);

  try xlist.append("Italie");
  try xlist.append("Rome");

  MlistArg.appendAssumeCapacity(.{
        .buf = xlist
    });


  std.debug.print("\n\r MlistArg  len >{d}<\n\r",.{MlistArg.len});


  i= 0 ;

  while (i < MlistArg.len) :(i += 1) {
    xlist = MlistArg.items(.buf)[i];

    for (xlist.items) | field | {
      std.debug.print("\n\r Field >{s}<\n\r",.{field});
    }
  }

result:
MlistArg len >3<

Field >France<

Field >Paris<

Field >Espagne<

Field >Madrid<

Field >Italie<

Field >Rome<

Uh, did you perhaps intend to reply to a different thread?

sujet :got some problems to setup/initialize multidimensional slices from strings. !!!

“QWERTZUIOASDFGHJKPYXCVBNML”, “WE”
“AZERTY…” “WO”

but we can associate a more complex structure