Understanding the string represented data type in Matlab - string

I have a String like '12,23,43,erogol,bla,3.4' and I want to parse it and see which type of values are in this string. For example if I give that string to function I expect to have a vector like output=["integer,integer,integer,string,string,double"] as the return of the function.
How could I do it in matlab ?

This can be done very easily using regular expressions.
input = '-12,12,0,erogol,bla,3.4,-3.4';
First replace the strings with 'string'.
Next catch the doubles
Finally catch the integers:
Example:
output = regexprep(input, '[a-zA-Z]*', 'string');
output = regexprep(output, '[-]*[0-9]*[.][0-9]*', 'double');
output = regexprep(output, '[-]*[0-9]*', 'integer');
Output now contains `integer,integer,integer,string,string,double,double'
Which you can split into a cell array using:
varTypes = regexp(output, ',', 'split');

Related

System Verilog function that takes any number of strings as an argument?

I want to have a SystemVerilog function that can accept any number of string arguments, this function is essentially a wrapper around the in-built $display as such I want users to be able to call the function in the same way, passing any number of strings into myfunc() as follows:
myfunc(stringVar1, "literalString", stringVar2, stringVarN, intVarToConvertToString);
My current implementation is to just have a single string arg and use the SystemVerilog concatenation operator to concatenate all the strings I wish to pass; e.g.
myfunc({stringVar1, "literalString", stringVar2, stringVarN, intVarToConvertToString});
however when I try to pass in a numeric type like int as a concatenated string, it isn't being converted into a string representation of its value correctly, and the � character is printed in its place. I have also tried to perform an explicit string cast on the non-string types being concatenated but to no avail.
I know that if I got all of the arguments to my function separately I can then just pass them directly to $display which does correctly convert the number types to their string representation to display them. Does anyone know how I would do this in System Verilog?
If you want to convert an integer's value into a string representation, or more specifically, a "string" of ASCII digits, you need to use a formatting function, like $sformatf("%d",intVarToConvertToString) to convert a value to a decimal representation, or you can combine the concatenation and formatting into a single expression.
myfunc($sformat("%s%s%s%d",{stringVar1, "literalString"}, stringVar2, stringVarN, intVarToConvertToString);
The concat operator {} converts its contents in a stream of bits.
There are no variable arguments in verilog/system verilog functions.
There is no way to wrap variable args in a $display.
So your best bet is using dynamic or associative arrays of strings or queues.
The following is an example of a possible use of an associative array:
package pkg;
function void multstr(string args[int]);
for(int i = 0; i < args.num; i++) begin
$display("arg%0d: %s", i, args[i]);
end
endfunction
endpackage
module testme();
string strings[int];
initial begin
strings[0] = "hello ";
strings[1] = "world";
pkg::multstr(strings);
end
endmodule
Create a function with a dynamic array port like this:
module tb ();
// function arg is a dynamic array of strings
function void printStrings(string mystrings[]);
foreach(mystrings[i])
$display("",mystrings[i]);
endfunction
// dynamic array of string
string names [];
initial
begin
names = new[2];
names = '{"bob","joe"};
printStrings(names);
$display("-----");
// make the d-array 1 bigger
names = new[names.size() + 1](names);
names = '{"bob","joe","bill"};
printStrings(names);
end
endmodule
Which produces
bob
joe
-----
bob
joe
bill
The same thing could be done using a queue as a function port rather than a d-array.

How to convert a variable to a raw string?

If I have a string, "foo; \n", I can turn this into a raw string with r"foo; \n". If I have a variable x = "foo; \n", how do I convert x into a raw string? I tried y = rf"{x}" but this did not work.
Motivation:
I have a python string variable, res. I compute res as
big_string = """foo; ${bar}"""
from string import Template
t = Template(big_string)
res = t.substitute(bar="baz")
As such, res is a variable. I'd like to convert this variable into a raw string. The reason is I am going to POST it as JSON, but I am getting json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 620 (char 619). Previously, when testing I fixed this by converting my string to a raw string with: x = r"""foo; baz""" (keeping in line with the example above). Now I am not dealing with a big raw string. I am dealing with a variable that is a JSON representation of a string where I have replaced a single variable, bar above, with a list for a query, and now I want to convert this string into a raw string (e.g. r"foo; baz", yes I realize this is not valid JSON).
Update: As per this question I need a raw string. The question and answer flagged in the comments as duplicate do not work (res.encode('unicode_escape')).

Specman string: How to split a string to a list of its chars?

I need to split a uint to a list of bits (list of chars, where every char is "0" or "1", is also Ok). The way I try to do it is to concatenate the uint into string first, using binary representation for numeric types - bin(), and then to split it using str_split_all():
var num : uint(bits:4) = 0xF; // Can be any number
print str_split_all(bin(num), "/w");
("/w" is string match pattern that means any char).
The output I expect:
"0"
"b"
"1"
"1"
"1"
"1"
But the actual output is:
0. "0b1111"
Why doesn't it work? Thank you for your help.
If you want to split an integer into a list of bits, you can use the %{...} operator:
var num_bits : list of bit = %{num};
You can find a working example on EDAPlayground.
As an extra clarification to your question, "/w" doesn't mean match any character. The string "/\w/" means match any single character in AWK Syntax. If you put that into your match expression, you'll get (almost) the output you want, but with some extra blanks interleaved (the separators).
Regardless, if you want to split a string into its constituting characters, str_split_all(...) isn't the way to go. It's easier to convert the string into ASCII characters and then convert those back to string again:
extend sys {
run() is also {
var num : uint(bits:4) = 0xF; // Can be any number
var num_bin : string = bin(num);
var num_bin_chars := num_bin.as_a(list of byte);
for each (char) in num_bin_chars {
var char_as_string : string;
unpack(packing.low, %{8'b0, char}, char_as_string);
print char_as_string;
};
};
};
The unpack(...) syntax is directly from the e Reference Manual, Section 2.8.3 Type Conversion Between Strings and Scalars or Lists of Scalars

Extracting Specific Strings From Text

I am trying to extract specific strings from my text. I have a string similar to this:
"blablabla(/)Hello Bob(|)bla(/)Hi(|)blablaba"
and I am trying to concatenate a string array of text between (/) and (|) but I cannot figure out a efficient way to do this. In this example I would want to return "Hello Bob" and "Hi". How can I effectively extract specific strings from a string in Swift?
Using a functional approach, you can:
split the string by (/), which returns an array of strings
for each element of the array, transform it into an array of strings split by (|)
filter the resulting array including only values (arrays) having more than one element
transform each element (array) of the resulting array into its first element
This is the code:
let array = string.componentsSeparatedByString("(/)")
.map { $0.componentsSeparatedByString("(|)") }
.filter { $0.count > 1 }
.map { $0[0] }
Given this input string:
let string = "blablabla(/)Hello Bob(|)bla(/)Hi(|)blablaba"
the result is:
["Hello Bob", "Hi"]

Horde_Text_Diff string comparison library only comparing 1st character of strings

I am using Horde_Text_Diff to compute the difference between two strings. Sample code is as follows:
$check_diff = new Horde_Text_Diff( 'auto', array('asdf','asd11') );
$renderer = new Horde_Text_Diff_Renderer_Inline();
echo $renderer->render($check_diff);
This echoes nothing. The correct behaviour would be to show a difference at character 4.
If I change the comparison array from array('asdf','asd11') to, for instance, array('asdf','12345'), then it will output a1. In other words, it seems only to be comparing the first character. Any ideas?
When I try this, I get two warnings:
PHP Warning: array_walk() expects parameter 1 to be array, string given in /usr/share/php/Horde/Text/Diff/Engine/Native.php on line 33
PHP Warning: array_walk() expects parameter 1 to be array, string given in /usr/share/php/Horde/Text/Diff/Engine/Native.php on line 34
I.e., something is getting strings where it expects arrays.
That's because, rather than passing (an array containing) two strings to Horde_Text_Diff(), you should pass (an array containing) two arrays-of-strings (where each string represents a line of text).
If the actual strings you're currently trying to pass in contain multiple lines of text, then you can split them into arrays-of-strings using explode(), e.g.:
$a = "foo\nbar\nbaz";
$b = "foo\nqux\nbaz";
$a_lines = explode("\n", $a);
$b_lines = explode("\n", $b);
$check_diff = new Horde_Text_Diff( 'auto', array($a_lines, $b_lines) );
$renderer = new Horde_Text_Diff_Renderer_Inline();
echo $renderer->render($check_diff);
which outputs:
foo
<del>bar</del><ins>qux</ins>
baz

Resources