CAPL associative array with string types - string

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

Related

What is the difference between strings and characters in Matlab?

What is the difference between string and character class in MATLAB?
a = 'AX'; % This is a character.
b = string(a) % This is a string.
The documentation suggests:
There are two ways to represent text in MATLAB®. You can store text in character arrays. A typical use is to store short pieces of text as character vectors. And starting in Release 2016b, you can also store multiple pieces of text in string arrays. String arrays provide a set of functions for working with text as data.
This is how the two representations differ:
Element access. To represent char vectors of different length, one had to use cell arrays, e.g. ch = {'a', 'ab', 'abc'}. With strings, they can be created in actual arrays: str = [string('a'), string('ab'), string('abc')].
However, to index characters in a string array directly, the curly bracket notation has to be used:
str{3}(2) % == 'b'
Memory use. Chars use exactly two bytes per character. strings have overhead:
a = 'abc'
b = string('abc')
whos a b
returns
Name Size Bytes Class Attributes
a 1x3 6 char
b 1x1 132 string
The best place to start for understanding the difference is the documentation. The key difference, as stated there:
A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store short pieces of text as character vectors, such as c = 'Hello World';.
A string array is a container for pieces of text. String arrays provide a set of functions for working with text as data. To convert text to string arrays, use the string function.
Here are a few more key points about their differences:
They are different classes (i.e. types): char versus string. As such they will have different sets of methods defined for each. Think about what sort of operations you want to do on your text, then choose the one that best supports those.
Since a string is a container class, be mindful of how its size differs from an equivalent character array representation. Using your example:
>> a = 'AX'; % This is a character.
>> b = string(a) % This is a string.
>> whos
Name Size Bytes Class Attributes
a 1x2 4 char
b 1x1 134 string
Notice that the string container lists its size as 1x1 (and takes up more bytes in memory) while the character array is, as its name implies, a 1x2 array of characters.
They can't always be used interchangeably, and you may need to convert between the two for certain operations. For example, string objects can't be used as dynamic field names for structure indexing:
>> s = struct('a', 1);
>> name = string('a');
>> s.(name)
Argument to dynamic structure reference must evaluate to a valid field name.
>> s.(char(name))
ans =
1
Strings do have a bit of overhead, but still increase by 2 bytes per character. After every 8 characters it increases the size of the variable. The red line is y=2x+127.
figure is created using:
v=[];N=100;
for ct = 1:N
s=char(randi([0 255],[1,ct]));
s=string(s);
a=whos('s');v(ct)=a.bytes;
end
figure(1);clf
plot(v)
xlabel('# characters')
ylabel('# bytes')
p=polyfit(1:N,v,1);
hold on
plot([0,N],[127,2*N+127],'r')
hold off
One important practical thing to note is, that strings and chars behave differently when interacting with square brackets. This can be especially confusing when coming from python. consider following example:
>>['asdf' '123']
ans =
'asdf123'
>> ["asdf" "123"]
ans =
1×2 string array
"asdf" "123"

How to convert List of arrays to two dimensional array?

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.

InstallShield 5.5 - Array of strings

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

Go - Comparing strings/byte slices input by the user

I am getting input from the user, however when I try to compare it later on to a string literal it does not work. That is just a test though.
I would like to set it up so that when a blank line is entered (just hitting the enter/return key) the program exits. I don't understand why the strings are not comparing because when I print it, it comes out identical.
in := bufio.NewReader(os.Stdin);
input, err := in.ReadBytes('\n');
if err != nil {
fmt.Println("Error: ", err)
}
if string(input) == "example" {
os.Exit(0)
}
string vs []byte
string definition:
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text. A string may be empty, but not nil. Values of string type are immutable.
byte definition:
byte is an alias for uint8 and is equivalent to uint8 in all ways. It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.
What does it mean?
[]byte is a byte slice. slice can be empty.
string elements are unicode characters, which can have more then 1 byte.
string elements keep a meaning of data (encoding), []bytes not.
equality operator is defined for string type but not for slice type.
As you see they are two different types with different properties.
There is a great blog post explaining different string related types [1]
Regards the issue you have in your code snippet.
Bear in mind that in.ReadBytes(char) returns a byte slice with char inclusively. So in your code input ends with '\n'. If you want your code to work in desired way then try this:
if string(input) == "example\n" { // or "example\r\n" when on windows
os.Exit(0)
}
Also make sure that your terminal code page is the same as your .go source file. Be aware about different end-line styles (Windows uses "\r\n"), Standard go compiler uses utf8 internally.
[1] Comparison of Go data types for string processing.

How to find all cyclic shifted strings in a given input?

This is a coding exercise. Suppose I have to decide if one string is created by a cyclic shift of another. For example: cab is a cyclic shift of abc but cba is not.
Given two strings s1 and s2 we can do that as follows:
if (s1.length != s2.length)
return false
for(int i = 0; i < s1.length(); i++)
if ((s1.substring(i) + s1.substring(0, i)).equals(s2))
return true
return false
Now what if I have an array of strings and want to find all strings that are cyclic shift of one another? For example: ["abc", "xyz", "yzx", "cab", "xxx"] -> ["abc", "cab"], ["xyz", "yzx"], ["xxx"]
It looks like I have to check all pairs of the strings. Is there a "better" (more efficient) way to do that?
As a start, you can know if a string s1 is a rotation of a string s2 with a single call to contains(), like this:
public boolean isRotation(String s1, String s2){
String s2twice = s2+s2;
return s2twice.contains(s1);
}
Namely, if s1 is "rotation" and s2 is "otationr", the concat gives you "otationrotationr", which contains s1 indeed.
Now, even if we assume this is linear, or close to it (which is not impossible using Rabin-Karp, for instance), you are still left with O(n^2) pair comparisons, which may be too much.
What you could do is build an hashtable where the sorted word is the key, and the posting list contains all the words from your list that, if sorted, give the key (ie. key("bca") and key("cab") both should return "abc"):
private Map<String, List<String>> index;
/* ... */
public void buildIndex(String[] words){
for(String word : words){
String sortedWord = sortWord(word);
if(!index.containsKey(sortedWord)){
index.put(sortedWord, new ArrayList<String>());
}
index.get(sortedWord).add(word);
}
}
CAVEAT: The hashtable will contain, for each key, all the words that have exactly the same letters occurring the same amount of times (not just the rotations, ie. "abba" and "baba" will have the same key but isRotation("abba", "baba") will return false).
But once you have built this index, you can significantly reduce the number of pairs you need to consider: if you want all the rotations for "bca" you just need to sort("bca"), look it up in the hashtable, and check (using the isRotation method above, if you want) if the words in the posting list are the result of a rotation or not.
If strings are short compared to the number of strings in the list, you can do significantly better by rotating all strings to some normal form (lexicographic minimum, for example). Then sort lexicographically and find runs of the same string. That's O(n log n), I think... neglecting string lengths. Something to try, maybe.
Concerning the way to find the pairs in the table, there could be many better way, but what I came up as a first thought is to sort the table and apply the check per adjacent pair.
This is much better and simpler that checking every string with every other string in the table
Consider building an automaton for each string against which you wish to test.
Each automaton should have one entry point for each possible character in the string, and transitions for each character, plus an extra transition from the end to the start.
You could improve performance even further if you amalgated the automata.
I think a combination of the answers by Patrick87 and savinos would make a fair amount of sense. Specifically, in a Java-esque pseudo-code:
List<String> inputs = ["abc", "xyz", "yzx", "cab", "xxx"];
Map<String,List<String>> uniques = new Map<String,List<String>>();
for(String value : inputs) {
String normalized = normalize(value);
if(!uniques.contains(normalized)) {
unqiues.put(normalized, new List<String>());
}
uniques.get(normalized).add(value);
}
// you now have a Map of normalized strings to every string in the input
// that is "equal to" that normalized version
Normalizing the string, as stated by Patrick87 might be best done by picking the rotation of the string that results in the lowest lexographic ordering.
It's worth noting, however, that the "best" algorithm probably relies heavily on the inputs... the number of strings, the length of those string, how many duplicates there are, etc.
You can rotate all the strings to a normalized form using Booth's algorithm (https://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation) in O(s) time, where s is the length of the string.
You can then use the normalized form as a key in a HashMap (where the value is the set of rotations seen in the input). You can populate this HashMap in a single pass over the data. i.e., for each string
calculate the normalized form
check if the HashMap contains the normalized form as a key - if not insert the empty Set at this key
add the string to the Set in the HashMap
You then just need to output the values of the HashMap. This makes the total runtime of the algorithm O(n * s) - where n is the number of words and s is the average word length. The total space usage is also O(n * s).

Resources