I have a list of arrays where I added many arrays but all of them are the same size. Now I want convert this list to two dimensional array. I tried in this way:
List<Array^>^ vList = gcnew List<Array^>();
//some code where I add elements to vList
array<String ^, 2> ^ anArray = gcnew array<String ^, 2>(vList->Count, 5);
anArray = vList->ToArray();
But I have got this error:
a value of type "cli::array<System::Array ^, 1> ^" cannot be assigned to an entity of type "cli::array<System::String ^, 2> ^"
I don't know how to convert it.
You're going to have to iterate and copy all of the strings individually. However, the raw type Array^ isn't that convenient to work with, so you'll need to do something about that.
Basically, what you need to do is this:
for (int outer = 0; outer < vList->Count; outer++)
{
arrayOfSomeSort^ innerArray = vList[outer];
for (int inner = 0; inner < innerArray.Length; inner++)
anArray[outer, inner] = innerArray[inner];
}
Depending on how the rest of the program is, and what objects are actually in the List, there are a few options on what to do. Here are the options I see, in order of preference.
Instead of a List<Array^>^, have vList be a List<array<String^>^>^. If the List truly is a list of string arrays, then this option is probably the most correct. In this case, arrayOfSomeSort^ would be array<String^>^.
If vList can't change type, but it does indeed contain string arrays, then I'd have the local variable innerArray be of type array<String^>^, and do a cast as you pull it out of vList.
If the arrays in the list aren't string arrays, but instead are object arrays that happen to contain strings, then I'd have array<Object^>^ innerArray, and cast to that instead, and do a cast to String^ as you pull each string out of innerArray.
If none of those is appropriate, then you can leave innerArray as type Array^. You'll need to call the explicit method Array.GetValue(int) instead of using the [] indexer. As with the previous option, you'll need to cast each string as you pull it out of the inner array.
You've set the second dimension to 5 without checking the lengths of the inner arrays; I'm assuming you know something we don't, and are sure that there won't be anything larger than 5. If not, you'll need to iterate the list once to get the maximum array size, create the 2D array, and then copy the strings.
Related
I want to read a string in Matlab (not an external text file) which has numerical values separated by commas, such as
a = {'1,2,3'}
and I'd like to store it in a vector as numbers. Is there any function which does that? I only find processes and functions used to do that with text files.
I think you're looking for sscanf
A = sscanf(str,formatSpec) reads data from str, converts it according
to the format specified by formatSpec, and returns the results in an
array. str is either a character array or a string scalar.
You can try the str2num function:
vec = str2num('1,2,3')
If you have to use the cell a, per your example, it would be: vec=str2num(a{1})
There are some security warnings in the documentation to consider so be cognizant of how your code is being employed.
Another, more flexible, option is textscan. It can handle strings as well as file handles.
Here's an example:
cellResult = textscan('1,2,3', '%f','delimiter',',');
vec = cellResult{1};
I will use the eval function to "evaluate" the vector. If that is the structure, I will also use the cell2mat to get the '1,2,3' text (this can be approached by other methods too.
% Generate the variable "a" that contains the "vector"
a = {'1,2,3'};
% Generate the vector using the eval function
myVector = eval(['[' cell2mat(a) ']']);
Let me know if this solution works for you
Following this paper, I'm trying to create an associative array like this:
variables
{
char[30] translate[ char[] ];
}
It is exactly the same example in the paper. Problem comes when I try to put values to this associative array. For example:
on preStart
{
translate["hello"] = "hola";
}
That gives me a compilation error: "Error 1112 at (89,23): operand types are incompatible"
What I'm doing wrong?
VERSIONS: I'm using Vector CAPL Browser included with CANalyzer version 11.0 SP2
With associative fields (so-called maps) you can perform a 1:1 assignment of values to other values without using excessive memory. The elements of an associative field are key value pairs, whereby there is fast access to a value via a key.
An associative field is declared in a similar way to a normal field but the data type of the key is written in square brackets:
int m[float]; // maps floats to ints
float x[int64]; // maps int64s to floats
char[30] s[ char[] ] // maps strings (of unspecified length) to strings of length < 30
If the key type char[] is specified, all the character fields (of any size) can be used as key values. In the iteration the loop variable must then also be declared as char[]. Key comparisons, e.g. in order to determine iteration sequence, are then performed as character string comparisons, whereby no country-specific algorithms are used.
char[] is the only field type that can be used as a key type. Please bear in mind that you can not declare variables or parameters of the char[] type, with the exception of loop variables in the iteration.
Association between strings:
char[30] namen[char []];
strncpy(namen["Max"], "Mustermann", 30);
strncpy(namen["Vector"], "Informatik", 30);
for (char[] mykey : namen)
{
write("%s is mapped to %s", mykey, namen[mykey]);
}
I have an array which outputs the following:
charges = [5.00, 26.00, 8.00, 4.00, 4.00, -8.00, 54.00, 52.48]
When I try to perform a sum using this:
charges.sum()
It gives me:
5.0026.008.004.004.00-8.0054.0052.48
I am assuming I need to convert it from a string to a float so I did:
Float.valueOf((String) charges.sum())
and it gives me an error which states 'multiple points'.
My question is how do I add all of these figures up?
If your list is actually of strings, you can quickly do the conversion with sum()'s closure form.
charges.sum { it.toBigDecimal() }
It is unclear what your list has in it, it seems like the entries in your list are actually strings (since sum concatenates them), or something that prints a string value (has a toString method that returns a string showing the value so you think it is numeric even though it isn’t) . They are definitely not numeric, so convert each of them to a numeric type before summing:
charges.collect { new BigDecimal(it.toString()) }.sum()
(What your code was doing was concatenating the string values together, then converting that to a numeric type.)
You must delete the cast (String)
Float.valueOf(charges.sum())
I "inherited" an old InstallShield 5.5 project which I need to modify.
One change I need to do involves a list of strings I have to populate.
I am looking to to define an array of strings. I tried this:
STRING ListOfStrings[10];
But when I try this:
ListOfStrings[1] = "test";
I get an error: error C8038: numeric value required
But this does work:
ListOfStrings[1] = 123;
Looks like the declaration of the ListOfStrings is of an array of chars, not an array of strings.
That's right, the notation STRING str[10] declares an array of 10 characters. (STRING itself is a resizable array of characters.) InstallScript is not a modern language. It's somewhat of a cross between C and VB, but changes several things. If you want a list of strings, you probably need to use the List Processing Functions, in particular declaring and creating a list of strings:
LIST lst;
STRING szString;
// : : :
lst = ListCreate(STRINGLIST);
szString = "test";
ListAddString(lst, szString, AFTER);
// : : :
In addition you should use some error checking, like that shown on the ListAddString example.
In less common situations it may be useful to declare an array of POINTER objects (or optionally WPOINTER objects in later versions). (Update your question if you think that might be necessary).
I would like to make a list of strings in MATLAB using the example below:
x = ['fun', 'today', 'sunny']
I want to be able to call x(1) and have it return 'fun', but instead I keep getting 'f'.
Also, is there a way to add a string to a list without getting the list giving back a number where the string should be? I have tried using str2double and a few other things. It seems like both of these thing should be possible to do in MATLAB.
The easiest way to store a list of strings that have different lengths is to use cell arrays. For example:
>> x = {'fun', 'today', 'sunny'}; %# Create a cell array of strings
>> x{1} %# Get the string from the first cell
ans =
fun
It's kind of a kludgy workaround, but
x = strsplit('fun.today.sunny', ',')
produces a list with individual, callable strings.