简体   繁体   English

使用for循环将字符串变量分配给数组

[英]Assign string variable to array using for loop

I have a list of string variables and I want to assign all of these to an array, but the list is to much, is there any possible for me to using for loop to assign it. 我有一个字符串变量列表,我想将所有这些变量分配给一个数组,但是列表太多了,我是否有可能使用for循环来分配它。

var class_1 = "some description..";
var class_2 = "some description..";
var class_3 = "some description..";
var class_4 = "some description..";
var class_5 = "some description..";
.
. 
var class_100 = "some description..";

var classes = [];

for(var i = 0; i < 100; i++){

 //loop string variables to array    

}

I think it is not possible for me to assign 100 variables manually to the array. 我认为不可能将100个变量手动分配给数组。 Anyone know any technique? 有人知道任何技术吗? Thank you. 谢谢。

[UPDATE] It requires in SystemVerilog syntax, since I not familiar in that language, so I present the idea in Javascript. [UPDATE]由于我不熟悉SystemVerilog语法,因此需要使用SystemVerilog语法。

There is no way in SystemVerilog to build an array of values from a list of independently named variables without manually assigning each variable to an element of an array. 在SystemVerilog中,如果不将每个变量手动分配给数组的元素,则无法从独立命名的变量列表中构建值的数组。

classes[0] = class_0;
classes[1] = class_1;
classes[2] = class_2;
...

The fact that the identifiers used for variables names have a numerical sequence is of no consequence because you cannot access identifiers except by the exact name. 变量名称所使用的标识符具有数字序列的事实无关紧要,因为您不能访问标识符,除非使用确切的名称。
If you want to automate this, you will need to change the way the string variables are defined. 如果要自动执行此操作,则需要更改定义字符串变量的方式。 As people have already posted, this should been modeled using an array to begin with, perhaps using an associative array: 正如人们已经发布的那样,应该首先使用数组来建模,也许可以使用关联数组:

string classes[string];
classes["class_1"] = "some description..";
classes["class_2"] = "some description..";
classes["class_3"] = "some description..";

foreach(classes[name])
       // do something with classes[name]

Another option is to use a SystemVerilog class object for each variable 另一种选择是对每个变量使用SystemVerilog类对象

class stringvar;
  string m_name;
  static stringvar list[$];
  function new(string name);
    m_name = name;
    list.push_back(this);
  endfunction 
endclass

stringvar class_0 = new("some description..");
stringvar class_1 = new("some description..");
stringvar class_2 = new("some description..");
...
foreach(stringvar::list[item])
   // do something with item.m_name

Something like this: 像这样:

var class_1 = "some description..";
var class_2 = "some description..";
var class_3 = "some description..";
var class_4 = "some description..";
var class_5 = "some description..";

var class_100 = "some description..";

var classes = [];
for(var i = 0; i < 100; i++){

 classes.push(window['class_'+i])

}

You have 100 variables? 您有100个变量? You should sort that out first 你应该先整理一下

I think there is some truth to that comment. 我认为那句话有些道理。 But to answer your question: Assuming your variables follow the same naming convention that you example above shows, you can assign predefined variables with array values like so: 但是要回答您的问题:假设您的变量遵循上面示例所示的相同命名约定,则可以为预定义变量分配数组值,如下所示:

var a = []
for(var i = 0; i < num_vars; i++){
    var key = 'class_' + (i + 1)
    if (this[key])
        a.push(this[key])
}

You perhaps can use eval. 您也许可以使用eval。 something like this: 像这样的东西:

for (var i = 0; i < 100; i++) {
    var toEval = "classes.push(class_" + i + ")";
    eval(toEval);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM