I have an array say myArray = ["Manu","Anju","Zamya","Bijoy"]
How can i sort this array in alphabetical order using coffeescript?
Expected Output : myArray = ["Anju","Bijoy","Manu","Zamya"]
Can't see a problem, this is same as js sort function: myArray.sort()
Related
let var1 = "AnyCode".sorted()
print(var1.joined(separator:""))
ERROR: No exact matches in call to instance method 'joined'
I am trying to join the array after sorting the string. = "AnyCode"
I was expecting the output was = ACdenoy
But it is giving an error.
A Swift String is a collection of Characters, and sorted() applied to a collection returns an array with the collection elements in sorted order.
So var1 has the type [Character], and you can simply create a new string from that array with:
let var1 = "AnyCode".sorted()
print(String(var1)) // ACdenoy
Alternatively to Martin R's answer (but not better than that answer), you might have said
print(var1.map(String.init).joined())
...turning the characters to strings before trying to join the array elements.
I have 2 arrays - one has attribute called vcdName and the other has attribute called name. I want to remove from array 1 all entries where I find the value of vcdName in the second array. So:
array one
data...vcdName=a
data...vcdName=b
array two
data...name=a
I want to filter array one and remove the first entry (because vcdName value a is found in array two).
I understand I can use lodash functions and I tried the following. I believeusing is will return me those entries that match and I need to put in filteredArray those entries where no match is found in arraytwo.
filteredArray = _.filter(#arrayone, (vcd) -> vcd.vcdName is #arraytwo)
I hope I have not gotten too confusing. It feels this should be moderately easier than I am making it.
Generically I need to do this
for each entry in arrayone
for each entry in arraytwo
does entry from arrayone.vcdName == arraytwo.name - if yes then I do not want that entry from arrayone
I hope that helps
filter is in js core so I don't see why you'd need to use the lodash function, but anyway you can do it like this:
arraytwo_names = #arraytwo.reduce (dict, entry) ->
dict[entry.name] = true
dict
, {}
results = #arrayone.filter (x) ->
!arraytwo_names[x.vcdName]
The reduce is not strictly speaking necessary, since you could loop through arraytwo each iteration of the filter, but it is an optimization and makes the runtime O(N) and not O(N^2).
Suppose I have a cell like
A = {'erogol' 'grerol' 'biral'}
then I want to search inside for a particular string.
Is there any special function doing it?
One of the functions is strmatch:
index = strmatch('grerol',A,'exact');
It returns an array of indexes. It is now deprecated, and Mathworks recommend using strcmp instead
logicalIndexing = strcmp('grerol',A);
Another option is ismember :
[bIsMember,index]=ismember('grerol',A);
Another option is strfind :
indexes = strfind(A,'grerol');
Last but not least,
booleanIndexes = cellfun(#(x)(isequal(x,'grerol')),A);
As input to a function, I am getting an array of target elements, T, and an array of structs S where each one has a .elems field, which is a list of integers (elements).
I'm sure there's a simple way to do this in Matlab. How do I get the indices i of all structs where a specific element t of T is in S(i).elems contains t?
So I think you'll need to do this with an arrayfun. I did:
S = ... (1-by-N array of structs);
T = ... (1-by-K array of numbers);
indices = find(arrayfun(#(i)any(ismember(T, S(i).elems)), 1:numel(S)));
any(ismember(T, S(i)elems)) tests is any of the things in T are in S(i).elems. The arrayfun repeats this for each struct in S. find extracts indices from the logical array that is returned by the arrayfun.
I have an object I defined with a method, childNodes(), which returns an array. When I do something like:
my #arr = obj->childNodes() I can clearly see that it can properly return an array.
My problem is that when I try to use this method to set the attribute of another class object, Perl decides I just want the length of childNodes() rather than the full array. This is not at all what I want and ruins everything. The code I'm using for this is:
$self->{'_arr'} = obj->childNodes()
How can I make this set $self->{'_arr'} to an array instead of just a scalar number?
Thanks in advance!
When you evaluate an array in scalar context, it returns the length of the array.
You want a reference to the array:
$self->{'_arr'} = [ obj->childNodes() ];
See perldoc perlref.