Given a string “ABCDE”, how do i find the index of occurrence of another string “C” in Golfscript?
? operator doesn’t seem to work (http://www.golfscript.com/golfscript/builtin.html#?):
“C” “ABCDE” ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s no way that
"C" "ABCDE" ?would work – if that did a string search, it would be looking for the first occurrence ofABCDEinC.However, in GolfScript strings are really a different presentation of arrays of integers.
"ABCDE"67?gives2because 67 is the Unicode codepoint forC.One slightly nicer approach which you might expect to work but doesn’t is (X)
This is rather counter-intuitive, but “correct”:
?is an order operation, and string has priority over array. Compare:The first gives 2, as expected, but the second gives -1 because the priority of string means that it’s searching for the array inside the string – and no array will ever be equal to an int representing a Unicode codepoint. However, these examples do point the way to another approach of reducing the strings to arrays of ints before using approach X.
Update
I sent an e-mail to flagitious suggesting a patch and the latest version of Golfscript has new behaviour for
string string ?andstring array ?. So if you update,"ABCDE""C"?should give2.