I have a randomly generated string which is generated at runtime.
I want to capture the value of that string into an array such that
the first word of the string becomes the first element in the array,
second word the second element
and so on.
Please help
In VBscript use the Split function:
dim myArray, myString
myString = "This is an example"
myArray = Split(myString)
Related
I am trying to store the longest resultant string after using the function strsplit unable to do so
eg: I have input strings such as
'R.DQDEGNFRRFPTNAVSMSADENSPFDLSNEDGAVYQRD.L'or
'L.TSNKDEEQRELLKAISNLLD'
I need store the string only between the dots (.)
If there is no dot then I want the entire string.
Each string may have zero, one or two dots.
part of the code which I am using:
for i=1:700
x=regexprep(txt(i,1), '\([^\(\)]*\)','');
y=(strsplit(char(x),'.'));
for j=1:3
yValues(1,j)=y{1,j};
end
end
But the string yValues is not storing the value of y, instead showing the following error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
What am I doing wrong and are there any suggestions on how to fix it?
The issue is that y is a cell array and each element contains an entire string and it therefore can't be assigned to a single element in a normal array yvalues(1,j).
You need yvalues to be a cell array and then you can assign into it just fine.
yValues{j} = y{j};
Or more simply
% Outside of your loop
yValues = cell(1,3);
% Then inside of your loop
yValues(j) = y(j);
Alternately, if you just want the longest output of strsplit, you can just do something like this.
% Split the string
parts = strsplit(mystring, '.');
% Find the length of each piece and figure out which piece was the longest
[~, ind] = max(cellfun(#numel, parts));
% Grab just the longest part
longest = parts{ind};
I'm trying to concatenate a series of strings in a loop into a variable array but the resulting strings are always within curly braces. Why does this happen, and how can I concatenate the string without them? Thanks
subs = {'abc001' 'abc002' 'abc003' 'abc004'};
for i = 1:size(subs,2)
subject = subs(i);
files_in(i).test = strcat('/home/data/','ind/',subject,'/test_ind_',subject,'.mat');
end
files_in(1)
% ans =
% test: {'/home/data/ind/abc001/test_ind_abc001.mat'}
I would like it to be:
test: '/home/data/ind/abc001/test_ind_abc001.mat'
subs is a cell array. If you index it using () notation, you will also get a cell array.
a = {'1', '2', '3'};
class(a(1))
% cell
To get the string inside the cell array you need to use {} notation to index into it.
class(a{1})
% char
When you use strcat with cell arrays, the result will be a cell array. When you use it with strings, the resut will be a string. So if we switch out (k) with {k} we get what you expect.
for k = 1:numel(subs)
subject = subs{k};
files_in(k).test = strcat('/home/data/ind/', subject, '/test_ind_', subject, '.mat');
end
A few side notes:
Don't use i as a variable. i and j are used in MATLAB to indicate sqrt(-1).
It is recommended to use fullfile to construct file paths rather than strcat.
when I use the function split to break a string into an array I get 2 different results.
if I type in the string in the code the return is an one dimensional array —array(0), array(1), array(2)
values_array = Array(Split("value1, value2", ","))
however, if read the value from a cell or something, the return is a two dimensional array — array(0,0), array(0,1), array(0,2)
values_array = Array(Split(row.Columns(2).Value, ","))
PS. in the line of code above "row" is dim as range
it probably looks stupid, but I tried it this way and it didn't work
values_array = Array(Split(Format(row.Columns(2).Value, "#"), ","))
Both Array(Split("value1, value2", ",")) and Array(Split(row.Columns(2).Value, ",")) create a two-dimensional array.
The Split function return an array.
The Array function take a variable number of parameters and make an array out of them.
So in both cases you are making an array with one element which is itself an array.
If you are more comfortable with a picture, here it is
where a has been set to Array(Split("value1, value2", ",")).
It's up to you to know how your program needs to store data, but you may consider removing the call to the Array function.
I am trying to fill an array of length 300 with the same value using VB here is my code:
Dim counter As Integer
If (value = True) Then
For counter = LBound(arrLowThresholds) To UBound(arrLowThresholds) Step 1
arrLowThresholds(counter) = LOG_TEST_LOW_THRESHOLD
arrHighThresholds(counter) = LOG_TEST_HIGH_THRESHOLD
Next
End If
The problem with this is that only the first element of the arrays is being filled.
Note that some of the variables are declared elsewhere that is the reason that the declaration is not visible.
arrLowThresholds and arrHighThresholds are the arrays while LOG_TEST_LOW_THRESHOLD and LOG_TEST_HIGH_THRESHOLD are the variables
I solved the problem by making a local array to use with the for loop. Then I copied the contents to the array I am actually using.
I have got array of strings like:
string [] foo = ["zxc", "asd", "qwe"];
I need to create string from them. Like:
"zxc", "asd", "qwe" (yes every elements need be quoted and separate with comma from another, but it should be string, but not array of strings.
How can I do it's in functional style?
import std.algorithm, std.array, std.string;
string[] foo = ["zxc", "asd", "qwe"];
string str = foo.map!(a => format(`"%s"`, a)).join(", ");
assert(str == `"zxc", "asd", "qwe"`);
std.algorithm.map takes a lambda which converts an element in the range into something else, and it returns a lazy range where each element is the result of passing an element from the original range to the lambda function. In this case, the lambda takes the input string and creates a new string which has quotes around it, so the result of passing foo to it is a range of strings which have quotes around them.
std.array.join then takes a range and eagerly concatenates each of its elements with the given separator separating each one and returns a new array, and since it's given a range of strings it returns a string. So, by giving it the result of the call to map and ", " for the separator, you get your string made up of the original strings quoted and separated by commas.
std.algorithm.joiner would do the same thing as join, except that it results in a lazy range instead of an array.
Alternatively, use std.string.format
Your format specifier should be something like this:
auto joinedstring = format("%(\“%s\",%)", stringarray)
this is untested as I'm on mobile, but it's something similar to it!