Get Multiple Locations of the Same String in Array - iphone

Is there any way to get the indices of the same string (that appears more than once) in a single array? I know I can find a specific string's location using:
[nameOfArray indexOfObject:#"apple"]
Of course, I could create a for loop essentially using the same code above and ignoring the previous "apples" found. I can't help to feel that there is a simpler (built-in) way to do this in objective-c. Am I right?
Thank you all in advance.

You could use indexesOfObjectsPassingTest with the "test" block being a block that tests for equality.

Related

Swift3 URL from URLComponents, how to add OR query items

Im constructing a URL from query items using URLComponents and I want to add some query items as OR conditions rather than AND. Im not sure what the proper terminology for this is. Anyway I would like the following, roughly
website.com/things?param1=thing&param2=thing|param3=thing|param4=thing
but appending query items i can only get
website.com/things?param1=thing&param2=thing&param3=thing&param4=thing
My goal is to check 3 different parameters for the term I pass in, and return any results that match from any of the 3. If I was constructing the url from a string, I could just use a pipe instead of ampersand (I think - please correct me if wrong), but Im using URLComponents and am not sure how to do this.
Perhaps Im going about this incorrectly. I dont have a ton of experience with this. If this is the wrong approach, please point me in the right direction. Im not sure how to word this question appropriately and that makes it hard to search for an answer
Im not sure what the proper terminology for this is
There is no terminology for it; it doesn't exist. What you're trying to do is nonstandard. There is no such thing as a query item OR condition. Standard separators are semicolon and ampersand, with ampersand used almost universally. You can't use a pipe to separate query items.
Thus, for example, if you paste website.com/things?param1=thing&param2=thing|param3=thing|param4=thing into the parser at http://www.freeformatter.com/url-parser-query-string-splitter.html, it doesn't know what to make of the pipes; it thinks that param2 must be thing|param3=thing|param4=thing.
Thus, URLComponents is not going to insert the pipe for you. Its goal and purpose is to make a valid URL, and you are attempting to make an invalid one.

Define variables in loops using loop indicies [duplicate]

I have a process which is repeated on a set of data stored in separate folders. Each time a certain folders data is processed I need new variable names as I need to results separate after the initial processing is finished for more processing.
For example at the start of each new block of the repeated function I declare sets of arrays
Set_1 = zeros(dim, number);
vectors_1 = zeros(dim, number);
For the next set of data I need:
`Set_2 = .........`
and so on. There is going to be alot of these sets so I need a way to automate the creation of these variables, and the use the new variables names in the function whilst maintaining that they are separate once all the functions are completed.
I first tried using strcat('Set_1',int2str(number)) = zeros(dim, number) but this does not work, I believe because it means I would be trying to set an array as a string. I'm sure there must be a way to create one function and have the variables dynamically created but it seems to be beyond me, so it's probably quite obvious, so if anyone can tell me a way that would be great.
I'd not do it like this. It's a bad habit, it's better to use a cell array or a struct to keep multiple sets. There is a small overhead (size-wise) per field, but it'll be a lot easier to maintain later on.
If you really, really want to do that use eval on the string you composed.
The MATLAB function genvarname does what you want. In your case it would look something like:
eval(genvarname('Set_', who)) = zeros(dim, number);
However, I would follow the recommendations of previous answers and use a cell or struct to store the results.
This sort of pattern is considered harmful since it requires the eval function. See one of the following for techniques for avoiding it:
http://www.mathworks.co.uk/support/tech-notes/1100/1103.html
http://blogs.mathworks.com/loren/2005/12/28/evading-eval/
If you insist on using eval, then use something like:
eval(sprintf('Set_1%d = zeros(dim, number);', number))

How to know if a PostfixExpression belongs to a for statement?

I'm using eclipse parser to work with expressions and statements in java code.
I have a function:
public boolean visit(PostfixExpression node)
which deals with Postfix expressoins, such ass i++;
Problem is i want to distinguish between a for statement postfix, and other postfixes.
I thought maybe i could get to the node's parent and somehow check if it's a for. Something like node.getParent()... but node.getParent() doesn't return an expression.
Any ideas how to recognize if the PostfixExpression belongs to a for loop?
Thanks
edit:
By "for statement postfix" i mean the postfix in the for loop's first line. Such as:
for(i=0;i<10;i++)
So i want to distinguish this i++ from other i++'s.
Can't you just call ASTNode.getParent() to see what kind of statement the expression is contained in?
I solved this by creating a for_updaters List (using node.updaters()) and updating it every time i visit a for loop (could also be nested loops). Also, whenever i come across a PostfixExpression (including for updaters), i add it to another List, and then delete from this List all similar occurrences that appear in for_updaters List. This way i'm only left with non-for-updaters Postfixes. This also works for me because every time i visit a for loop i clear both Lists, so no worries about duplicate variable names.
Note: node.updaters() returns the exact full expression: [i++]. But i only need i. It's easy to extract it by converting the updater to String and then use substring().

Accessing values in multiple structures with similar names

I'm reasonably new to Matlab, and have been trying to teach myself. I have looked for a similar question, but can't find one that's quite right.
In my workspace I have several structures with similar names. These structures will always start with the same word ('Base'), though the rest of the name will change ('1', '2', '3'), so for example Base1, Base2, Base3... etc. These variables were generated using the data cursor tool in a figure, so contain the fields Target, Position and DataIndex. I am only interested in the value in Base*.Position(1,1). I would like to extract this value from each structure, as many times as there are structures (in one instance there may be 6 structures, another time only 4).
I am considering using the eval function, but it seems to work on exact strings rather than only the first part of a name. Additionally, a lot of documentation seems to advise against using eval.
So far I have:
clearvar except 'Base*'
list_variables=who;
for i=1:length(list_variables)
BaseTS(i) = eval('Base1.Position(1,1)');
end
It's the for loop I'm stuck on, as I don't know how to generalise so it will extract the value .Position(1,1) for each different structure name.
Thanks in advance
Instead of having many structures called Base1, Base2 etc rather put your structure in an array. Then you could rather call Base(1).Position(1,1), Base(2).Position... etc. Your code will be more flexible and manageable this way.
So I suggest when you export using the data cursor, export to a variable called Base_temp and then immediately stick this into the next element of an array:
Base(end+1) = Base_temp
or even:
Position(end+1) = Base_temp.Position(1,1);
Then it's just a case of pressing up and enter after each time you export with the data cursor.
What you have read about avioding eval is correct, it's very rare (if ever) that eval is a good idea. It makes your code hard to read and very hard to debug. But since you're learning, this is how you could fix your loop. (But don't do this way, seriously don't, use arrays rather):
for i=1:length(list_variables)
BaseTS(i) = eval(['Base', num2str(i), '.Position(1,1)']);
end
in other words use string concatenation to build up your string and use the looping variable (i) to get the different numbers. You'll need num2str to convert fromthe number to the string. But don't do it this way. This is a bad way.
Dan's suggestion about avoiding eval is very valid. But if you decide to keep on the structures you have in your workspace, here's something without loops, but again cellfun seems to use loops internally. So, I guess this could be an alternative solution, with the not-so-popular eval -
list1 = who('Base*')
list2 = cellstr(strcat('BaseTS(',num2str([1:numel(list1)]'),')='));%%//'
ev1 = strcat(list2,list1,'.Position(1,1)');%%//'
cellfun(#evalc,ev1,'uni',0)

Kentico Nested Macro Comparison

I want to compare a url to a specific path to see if they match. I have tried so many variations and just can't get it to work, the two items I need to use are
{%CurrentDocument.RelativeURL.Replace("~","")%}
and
{&/{0}/{1}/{2}|(tolower)&}
In the current test scenario, both of these return the same string, however, when I put them together
{%CurrentDocument.RelativeURL.Replace("~","")|(equals){&/{0}/{1}/{2}|(tolower)&}|(truevalue)yes#%}
I get a false result displaying, I'm pretty positive it's because I can't nest a path expression inside an expression but not sure if there is another way? Any help would be appreciated.
Thanks
According to the documentation you should be able to substitute path macro with {%Path.path%}. Then there is no need to nest different types of macros but use the Path the same way as CurrentDocument.