Message Boards Message Boards

GROUPS:

Can string position preserve the order they were asked for?

Posted 18 days ago
144 Views
|
4 Replies
|
4 Total Likes
|

given the text string

text = "Today A, Tomorrow B, yesterday C"

and

StringPosition[text, {"B", "A", "C"}]

returns

{{7, 7}, {19, 19}, {32, 32}}

No matter what order {A, B, and C} are asked for it always returns them sorted, I need to preserve the order at all costs, is there some hidden command to do this? or maybe an alternative way preferably without doing 3 separate instances.

Regards

Paul.

4 Replies
StringPosition[text, #] & /@ {"B", "A", "C"}
Flatten[%, 1]
Posted 17 days ago

Thank Raspi, works perfectly.

You need to read the "Mathematica book" (help, but i like version 4.0 since it's formatted as a book not a disconnected set of tutorials that are now scattered). you need to understand functions that do not Hold[] arguments are Evaluated by kernel BEFORE the function sees them, and After as well, and at each step some function does that the kernel handles for the function (say, of Plus is used, the kernel must evaluate it).

But also that Mathematica keeps things in List[]. giving a function a List is better, but often you may give a function one argument at a time and store the results as List (and not all functions support List, either)

The person above instructed you (backwardsly) to do one argument at a time, which Mathematica accumulates as a List[] just as if Map[StringPosition, {...}] were used.

There are firm reasons why why things are "well ordered" by default. Consider trying to find parts of an equation: if the equation is not well ordered you cannot find it's parts since you cannot say where they might be in a jumble "stuff array". oppositely every part of an expression in Mathematica is perfectly ordered. You can find the term multiple factor exponent (etc) by level, by part, case, and etc. only if they have order.

ie: Level[expr,levelspec], gives a list of all subexpressions of expr on levels specified by levelspec. ie: FactorList[poly], gives a list of the factors of a polynomial, together with their exponents.

These basic necessities of doing mathematics all operate on the principle of ordering. (also, searching for things requires perfect ordering for efficiency, a computer science tip)

MEANWHILE. You can ask for a string position and store the result as {input,output} or f[input]:=output. you can then use Case or make a function which tells you (note it uses binary search, see above) which of your input got what output. or just have input[i] and output[i] and use i to crossreference

I did say check Options[StringPosition] didn't I ?

We don't know for sure if StringPosition is the "culprit". But it may be. It may be for "efficiency of massively large searches" it was done intently (the unwanted ordering). That I can't tell you.

Posted 17 days ago

John, thank you for the explanations etc it all makes sense, in regard to "well ordered" by default there are obvious reason why it is so, but I cant help think that during the search for the position of the items they were found in the order asked for, it's not until all items have been found are they then ordered and displayed. It would then make sense for there to be an option to return the positions in the order found. I obviously don't know the internal working of these things and it may not be so, but it sure would have been handy.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract