gpt4 book ai didi

c# - 将数组拆分为二维或两个数组

转载 作者:行者123 更新时间:2023-12-04 00:30:47 26 4
gpt4 key购买 nike

我有一个正在阅读的文本文件,用于提取寄存器值及其包含的内容。

从数组中取出的 2 行是:(完整列表约为 700)

0x0003 = 0x0069

0x0007 = 0x0078

我想将它们分成两个数组或一个二维数组,最好是哪个(我是数组的新手)

我的目标是搜索数组,例如寄存器 3,找到索引,然后从第二个数组对应的索引中提取信息。

到目前为止,这是我的代码,

  List<string> registerFullList1 = new List<string>();

for (int i = 0; i < 2000; i++)
{
string[] importStringArray1 = new string[2000];
importStringArray1[i] = importStringArray1[i] + objReader.ReadLine() + "\r\n";
//code to extract register info from string array
string listsplit1 = Regex.Match(importStringArray1[i], @"(?<= 0x)[0-9A-Fa-z\s\=]{13}").Value;


if (listsplit1.Contains("0x")) //code to add to list only registers and ignore empty lines
{
registerFullList1.Add(Convert.ToString(listsplit1));
}

}

int[] index = new int[2000]; // is there a way here that I don't have to assign 0,1,2,3 to each assignment?

index[0] = registerFullList1.FindIndex(x => x.StartsWith("0003 ="));
Register3.Text = Regex.Match(registerFullList1[index[0]], @"(?<= 0x)[0-9A-F]{4}").Value;

index[1] = registerFullList1.FindIndex(x => x.StartsWith("0007 ="));
Register7.Text = Regex.Match(registerFullList1[index[1]], @"(?<= 0x)[0-9A-F]{4}").Value;

这一切都没有问题,我在文本框中显示了寄存器内容。但我想要两个数组,这样更合适,一个有寄存器号,一个有内容。我想不通,如有任何帮助,我们将不胜感激。

更新

阅读答案后的最终代码,

    List<string> registerNumberList = new List<string>();
List<string> registerContentList = new List<string>();
List<string> registerFullList = new List<string>();

for (int i = 0; i < 2000; i++)
{
string[] importStringArray1 = new string[2000];
importStringArray1[i] = importStringArray1[i] + objReader.ReadLine() + "\r\n";

string listsplit1 = Regex.Match(importStringArray1[i], @"(?<= 0x)[0-9A-Fa-z\s\=]{13}").Value; // @"(?<== 0x)[0-9A-F]{4}"
string listsplit2 = Regex.Match(importStringArray1[i], @"(?<= 0x)[0-9A-Fa-z\s\=]{4}").Value;// pulls out the register number from original array
string listsplit3= Regex.Match(importStringArray1[i], @"(?<== 0x)[0-9A-Fa-z\s\=]{4}").Value;//pulls out register content from original array

if (listsplit1.Contains("0x"))
{
registerNumberList.Add(Convert.ToString(listsplit3));//makes a list with register numbers
registerContentList.Add(Convert.ToString(listsplit2) );//makes a list with register content
registerFullList.Add(Convert.ToString(listsplit2) + "=" + Convert.ToString(listsplit3));//the full list

}

}

Dictionary <string, string> registers = registerFullList.Select(line => line.Split('=')
.ToArray())
.ToDictionary(items => items[0], items => items[1]); //joins the register numbers and content into a dictionary with just 4 decimal values for each

list1.Text = String.Join("\r\n", registerFullList);



list2.Text = registers["0010"]; // pulls out register info

谢谢大家

最佳答案

我建议使用 Linq:

如果你坚持数组:

  int[][] result = File
.ReadLines(@"C:\myFile.txt")
.Select(line => line
.Split('=')
.Select(item => Convert.ToInt32(item, 16))
.ToArray())
.ToArray();

如果第一个索引是唯一(因此可以用作),您可以将数据具体化为字典:

  Dictionary<int, int> result = File
.ReadLines(@"C:\myFile.txt")
.Select(line => line
.Split('=')
.Select(item => Convert.ToInt32(item, 16))
.ToArray())
.ToDictionary(items => items[0], items => items[1]);

...

// value == 0x0078 (120)
int value = result[0x0007];

关于c# - 将数组拆分为二维或两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41542965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com