How can I join a list of strings in specman?
Question
I have a list that I want to print:
foo: list of string;
I want to create a string bar
that is the concatenation of the elements of foo
. In Perl I would do:
$bar = join " ", @foo;
The only way I can think of to do this in specman is:
var bar: string = "";
for each in foo {
bar = appendf("%s %s", bar, it);
};
This seems like it would have very poor performance, because it copies bar
onto itself for each element in foo
. Is there any better way to do this?
Solution
There is also a dedicated function for this:
str_join(list: list of string, separator: string) : string
I'm sure help str_join
will give you the details. There are also other useful functions like str_match
, str_split
which you may like.
As an additional hint, maybe you should print yourself the e Language Quick Reference, see http://www.cadence.com/Community/blogs/fv/archive/2009/06/19/send-us-suggestions-for-updating-the-e-specman-quick-reference-card.aspx.
OTHER TIPS
While writing the question I stumbled across the to_string()
method. I can use:
var bar: string = foo.to_string();
This is the equivalent of Perl's:
$bar = join "\n", @foo;
If I want to use spaces I can use:
var bar: string = str_replace(foo.to_string(), "\n", " ");