0

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.

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. 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.

helloworld1234
  • 327
  • 1
  • 8
  • 19

4 Answers4

2

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.

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

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
dave_59
  • 39,096
  • 3
  • 24
  • 63
0

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])

}
aring
  • 3,422
  • 2
  • 22
  • 29
0

You have 100 variables? 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])
}
jusopi
  • 6,791
  • 2
  • 33
  • 44
0

You perhaps can use eval. something like this:

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