Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to parse string and call array using valueForKeyPath:
ex:
NSArray *myArray = #[1,2,3,4,5,6,7,8];
I have a string's as below
myArray[], myArray[3],myArray[1..3]
when i evaluate these strings i want output like below
evaluate(#"myArray[]")
output:#[1,2,3,4,5,6,7,8]
evaluate(#"myArray[3]")
output:4
evaluate(#"myArray[1..3]")
output:#[2,3,4]
I am already using custom function using valueForKeyPath: for calling methods and functions. I am stuck with array and parameters
You have shown nothing you have attempted, so it is unclear what your problem is – you appear to know about things (valueForKeyPath:, regular expressions) you could build a solution from.
Here is an outline of a solution:
Your strings all appear to be of one of three forms: an array name followed by zero, one or two integers in square brackets. You can break this up to extract the one to three important parts using (at least) NSRegularExpression or NSScanner. If you pick NSRegularExpression just be careful writing the pattern as [, ], and . are special characters and will need appropriate escaping – just read the documentation.
Once you have your three parts you can obtain the value of the array using valueForKey: assuming it is either a property or an instance variable.
If you have matched/scanned one or two indexes then you can use the methods of NSArray to obtain the object(s), you might find NSIndexSet useful here.
That's it. If you try to do this and get stuck edit your question showing what you have tried, state what is broken, and ask for help; someone will probably be able to help further.
HTH
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I am wanting to generate the same random sequence (of numbers or characters) based on a given "seed" value.
Using the standard Randomize function does not seem to have such an option.
For example in C# you can initialize the Random function with a seed value (Random seed c#).
How can I achieve something similar in Delphi?
You only need to assign a particular value to the RandSeed global variable.
Actually, I'm almost surprised you asked, because you clearly know of the Randomize function, the documentation for which states the following:
The random number generator should be initialized by making a call to Randomize, or by assigning a value to RandSeed.
The question did not mention thread safety as a requirement, but the OP specified that in comments.
In general, pseudorandom number generators have internal state, in order to calculate the next number. So they cannot be assumed to be thread-safe, and require an instance per thread.
For encapsulated thread-safe random numbers, one alternative is to use a suitably good hash function, such as xxHash, and pass it a seed and a counter that you increment after each call in the thread.
There is a Delphi implementation of xxHash here:
https://github.com/Xor-el/xxHashPascal
For general use, it's easy to make several versions in 1, 2 or 3 dimensions as needed, and return either floating point in 0..1 or integers in a range.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is the structural rule of something like this? I'm newer to programming and I don't know the technical term for the ".something's" (methods?).
But, in this example, there are 5 (to_s, chars, map, join, and to_i).
num.to_s.chars.map{|x| x.to_i**2}.join.to_i
Basically, all I am wondering is, what is the structure to building these? I've tried doing some similar and have received errors. So, is there a specific order or structure to these? And is the correct term method?
Ideally you should first get fundamental of ruby language. Ruby is one of the easiest language to get hold on. Checkout https://try.ruby-lang.org and you will better understand following.
It's an expression where there is chain of methods being called on the result of each expression.
Assuming num is an integer, see the comment below
num
.to_s # to_s on any ruby object converts it to string
.chars # returns individual characters in string array
.map { |x| # iterates over each number character in array
x.to_i**2 # and convert each character to integer and sqare it( ** is exponent operator)
}
.join # map returns new array and join/conctenate each number
.to_i # convert it back to integer
so if num is 123, it returns 149 which essentially each number is squared.
You can try yourself by running this code one by one in irb
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have some alphanumeric strings. From that I have to find out those strings which satisfy the following condition,
There should be only one character in the whole string and that should be 'e'
'e' should not present at the beginning or end of the string it should be present at the middle.
I want to pick strings like 43e4234,435345e5
I can do the same thing in ruby, but as i have huge number of strings i want to go with regular expression only
This should work:
/\A[^a-z]+e[^a-z]+\z/i
It means :
Beginning of the string
at least one non-letter
'e'
at least one non-letter
end of string
Here's an example :
https://regex101.com/r/H9oza7/1
Use /^[^a-z]+e[^a-z]+$/im if you want to match lines inside a string.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm struggling with what I imagine is fairly simple - but I need to create a string by joining various strings and string vars together - this is what i have so far -
_msgTxt = #"I have achieved great results with my instructor%#", _usrName, #"Check her out here", _usrURL;
any tips on where i'm going wrong? I'm hoping to achieve a long string ie ' I have achieved great results with my instructor Zoe Edwards. Check her out here http://www.nme.com" which could be posted to social media channels.
Cheers
You'll need to use stringWithFormat.
Example:
_msgTxt = [NSString stringWithFormat:#"I have achieved great results with my instructor %#. Check her out here %#", _usrName, _usrURL];
One thing to keep in mind using the example above is that the objects/variables provided should appropriately use the description method to output user visible strings. NSString does, but other objects may output something which isn't user friendly.
If this is the case, you should use an NSString object within the parent object to display the information (You'd need to create this yourself; _usrURL.userFriendlyString for example).
Use [NSString stringWithFormat:*enter you stuff here*];
While stringWithFormat: will work as proposed by the other answers, it isn't very efficient if you just want to concatenate a number of strings in a set order. The power of stringWithFormat: comes from the contents of the format and the ability to reorganise and 'format' contents with the parameter specifiers. But it comes with a cost because the format string has to be parsed and processed.
For simple string concatenation, use NSMutableString and the appendString: method. (and note that you can also use appendFormat: of you have one part that needs it...).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Once I use [NSString hash] and get a NSUInteger, is there any way I can use that NSUInteger and turn it back into the original NSString? Apple doesn't really say anything about the implementation of the hash method in the docs.
FYI: I'm trying to store identifierForVendor as a NSNumber (specifically in either the major or minor property of a CLBeacon).
No. The hash is 32 or 64 bits, a string can be much longer, so it is inherently lossy, and the hash values are not unique (the same hash corresponds to multiple strings).
Actually, hash is not supposed to be de-coded. You may want to read something like http://en.wikipedia.org/wiki/Hash_function
Apple says "If two string objects are equal (as determined by the isEqualToString: method), they must have the same hash value". That's all you can get.
If you want to store it for later comparisons, then you should hash the both NSStrings & compare the resulting NSUIntegers