Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.3K Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Use FirstPositioning[] multiple times to separate and organise an txt file?

Posted 7 years ago
POSTED BY: Kim Hempel
3 Replies

Kim,

I’m having trouble understanding what your file looks like and what you want to do with the data. There are many text formats and many ways to separate the columns. Please post a small example of your text file so we can see the actual format and then give an example of what you would like to obtain.

Regards,

Neil

POSTED BY: Neil Singer
Posted 7 years ago
POSTED BY: Kim Hempel

Kim,

Is the txt coming in as a list of words? or is it a string of words. If it is a string of words then FirstPosition will not work. You need to use String searching instead of list searching.

I'll assume you are using a list of words like this:

wordlist = {"aaa", "bbb", "ccc", "bbb", "ddd", "eee", "fff", "ggg", "hhh", 
  "fff", "iii"}

The operation you describe is recursive -- you want to find a string starting at a word and search within that substring.

First we find a substring with one key word (the keyword is in a list all by itself)

finder[lst_, {lastone_}] := 
 Drop[lst, First[FirstPosition[lst, lastone]]]

Now overload the function to operate on the first keyword and recursively send the result to the function for the rest of the keywords:

finder[lst_, toFind_List] := 
 finder[finder[lst, {First[toFind]}], Rest[toFind]]

Now we can use it:

keywords = {"bbb", "ddd", "fff"};

finder[wordlist, keywords]

To get

{"ggg", "hhh", "fff", "iii"}

We searched for the "bbb", then searched for the "ddd" in the remaining string, then searched for the "fff". To verify what is happening you can call finder sequentially:

In[26]:= n1 = finder[wordlist, {"bbb"}]

Out[26]= {"ccc", "bbb", "ddd", "eee", "fff", "ggg", "hhh", "fff", \
"iii"}

In[27]:= n2 = finder[n1, {"ddd"}]

Out[27]= {"eee", "fff", "ggg", "hhh", "fff", "iii"}

In[28]:= n3 = finder[n2, {"fff"}]

Out[28]= {"ggg", "hhh", "fff", "iii"}

Regards,

Neil

*Edit: simplified the finder definition

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