-1

I have a txt file which was copied to Supporting Files of my Xcode project.The data in txt file is of format:

abacus@frame with balls for calculating

abate@to lessen to subside

abdication@giving up control authority

aberration@straying away from what is normal

....................around 4000 lines

I have successfully extracted data from the file using the below code:

NSString *greFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grewords" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
self.greWordsArray = [NSMutableArray arrayWithArray:[greFileString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

When I print greWordsArray,I could see the below output in log

"abacus@frame",
with,
balls,
for,
calculating,
"",
"abate@to",
lessen,
to,
subside,
"",
"abdication@giving",
up,
control,
authority,
"",
"aberration@straying",
away,
from,
what,
is,
normal,
"",

But I want the values in two separate arrays,one holding abacus,abate,abdication,authority aberration and other array with frame with balls for calculating,to lessen to subside,giving up control,straying away from what is normal i.e. one array holding string before @ symbol and one with after @ symbol

I know there are several methods like checking for special character method,string by replacing occurrences of string,using character set,but the fact is since my string greFileString is a bundle holding multiple strings,if I try any of these methods only abacus is getting added to array,but I want abacus,abate,abdication,aberration to be added to array.

EDIT

Following suggestion of H2CO3,I have implemented the following way:

NSString *greFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grewords" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
NSArray *greData = [NSArray arrayWithArray:[greFileString componentsSeparatedByString:@"@"]];
self.greWordsArray = [NSMutableArray array];
self.greWordHints = [NSMutableArray array];
for (NSString *greWord in greData)
{
   if ([greWord characterAtIndex:0] == (unichar)'@')
   {
     [greWordHints addObject:greWord];
   }
   else
   {
     [greWordsArray addObject:greWord];
   }
}
NSLog(@"gre words are %@",greWordsArray);
NSLog(@"gre hints are %@",greWordHints);

Here is the logged output:

gre words are (
    abacus,
    "frame with balls for calculating
\nabate",
    "to lessen to subside
\nabdication",
    "giving up control authority
\naberration",
    "straying away from what is normal
\nabet",
    "help/encourage somebody (in doing wrong)
\nabeyance",
    "suspended action
\nabhor",
    "to hate to detest

gre hints are (
)

Can someone please guide me on this?

Eshwar Chaitanya
  • 942
  • 2
  • 13
  • 34
  • Consider using a plist. http://stackoverflow.com/questions/7059966/how-to-use-plist-in-ios-programming – KishoreK May 23 '13 at 06:38
  • @KishoreK I am already aware of using plist,but it is quite easy for some one to handle around 100 or 200 words,actually the txt file contains around 4000+ lines,so I didn't have any other alternate than fetching the data from txt files and storing to array :) – Eshwar Chaitanya May 23 '13 at 06:45
  • @EshwarChaitanya Why couldn't you store 4000 entries in a plist? –  May 23 '13 at 06:46
  • @H2CO3 I need to enter all 4000 entries in plist,which is time consuming and a bit of burden for me,as I have to maintain separate plists,one for words and other for their hints,words are before @ symbol and hints are after @ symbol in all 4000 strings,please once check in post for data format in txt file,or can I know how to convert a txt file to plist?? – Eshwar Chaitanya May 23 '13 at 06:58
  • @EshwarChaitanya 1. Why use separate property lists when you can have dictionaries? 2. Once you have the array of strings, you just write them out from the `NSArray` or you wrap the array into a dictionary and you write out that dictionary and boom you have a property list... I can't believe researching this is that hard... –  May 23 '13 at 07:03
  • @H2CO3 So you mean to say,converting txt file to array,then adding the array to dictionary and finally adding that dictionary to plist,doesn't the process seem a lengthy one? – Eshwar Chaitanya May 23 '13 at 07:10
  • @EshwarChaitanya The `NSDictionary` class does it in under 0.1 seconds... and you do it only once anyway... –  May 23 '13 at 07:11
  • Even another 5-10 down votes doesn't make much difference to me,people should understand not every one is good at R&D or at coding,but I kindly request them to specify reason in comment while down-voting!! – Eshwar Chaitanya May 23 '13 at 07:33

2 Answers2

1

It's quite trivial: if the first character of the string is a '@', then put it in the one array, else put it in the other one.

NSArray *words = [greFileString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableArray *at = [NSMutableArray array];
NSMutableArray *noAt = [NSMutableArray array];

for (NSString *s in words)
    if ([s characterAtIndex:0] == (unichar)'@')
        [at addObject:s];
    else
        [noAt addObject:s];

Disregard the above - OP was lying to me >.< The text file actually consists of lines in which an at-symbol delimits the word and the explanation, i. e.

word1@explanation one
word2@explanation two

etc. This means that first the lines should be retrieved (perhaps using - [NSString componentsSeparatedByString:]), then each line is to be split into two part (the same method is useful here too).

  • Thanks for the answer,but it is crashing at line string characterAtIndex,throwing exception:range or index out of bounds – Eshwar Chaitanya May 23 '13 at 06:53
  • @EshwarChaitanya That indicates an empty string (perhaps google the exception message!?) –  May 23 '13 at 07:01
  • Surprising to hear that,because greFileString is not empty,when logged,I could see the output as same as that in txt file i.e.: string is abacus@frame with balls for calculating abate@to lessen to subside abdication@giving up control authority aberration@straying away from what is normal abet@help/encourage somebody (in doing wrong) abeyance@suspended action abhor@to hate to detest abide@be faithful to endure abjure@promise or swear to give up etc. – Eshwar Chaitanya May 23 '13 at 07:07
  • @EshwarChaitanya An empty **string,** not an empty file. Have a look at the last entry in your logged out data. It's `""`. That's an empty string. –  May 23 '13 at 07:08
  • Yeah I do acknowledge the same,but it is probably because of using character set to add the bundle string to array,do you know how to add the bundle string i.e. the format mentioned in output above in comment,if so please tell me – Eshwar Chaitanya May 23 '13 at 07:13
  • @EshwarChaitanya Now I couldn't make sense of this question. –  May 23 '13 at 07:14
  • My txt file is in the format: http://pastebin.com/SDT7e152 ,so when I googled for ways to convert the file in to plist,array etc. I found out a solution of placing in array,i.e. with the help of string which uses bundle to get all the data using contents of file.Then placing in array would be using string components separated by method.But that is changing the nature of original output i.e. as mentioned in logged output data.So I was trying to ask you how to add the txt file to array without changing the type of data. – Eshwar Chaitanya May 23 '13 at 07:23
  • Consider me as hopeless and please help me,I have implemented what you said,adding array to dictionary,even then I get output in some other format: re dictionary is { greWordsArray = ( "abacus@frame", with, balls, for, calculating, "", "abate@to", lessen, to, subside, "", what is this for?? I want words before @ in one array and after @ in other,please understand – Eshwar Chaitanya May 23 '13 at 10:51
  • @EshwarChaitanya In other words, you were not describing the problem and the format of your text file correctly. You can then use the `componentsSeparatedByString:` method of `NSString` in this case. **Don't blame on me that you failed to explain your problem in a comprehensible manner.** –  May 23 '13 at 11:21
  • ,Regret that I couldn't make my question precise and understandable for you,I have tried string componentsSeparatedByString i.e. self.greWordsArray = [NSMutableArray arrayWithArray:[greFileString componentsSeparatedByString:@"@"]]; and got output as: gre words are ( abacus, "frame with balls for calculating \nabate", "to lessen to subside \nabdication", "giving up control authority \naberration", "straying away from what is normal \nabet", "help/encourage somebody (in doing wrong) \nabeyance", "suspended action ,now what to do – Eshwar Chaitanya May 23 '13 at 11:40
  • @EshwarChaitanya Now you can read the edit in my answer and have a coffee and some sleep, perhaps you could grasp all this better tomorrow... –  May 23 '13 at 11:41
  • Yeah I got your point regarding using the same method yet again to split each line in to 2,how could I do that,I mean this time around we have array instead of string,components separated by string method is not available in array na,that's the reason why I was forced to comment again,thanks for your concern and help,but I need to rush in progress of my project :) – Eshwar Chaitanya May 23 '13 at 11:49
  • Oh the method you were mentioning earlier in answer,fine,will try and get back to you – Eshwar Chaitanya May 23 '13 at 11:52
  • Sorry H2CO3,I have edited my post,could you kindly have a glimpse at it,what happens is the output is same as in txt file without string split :( – Eshwar Chaitanya May 23 '13 at 13:24
  • Solved my problem,please check my answer above,your answer certainly helped me,hence I gave +1,but will accept my answer,sorry for that :) – Eshwar Chaitanya May 24 '13 at 03:48
  • I don't know whether your intention was to taunt me,but practically speaking you guided me well,but regardless of whether the fault is with me or you,I couldn't understand your solution well,more over you didn't specify the tactic of adding words and hints to arrays using objectAtIndex method,that's the reason why I told I will accept my answer,as I have made it clear and precise for any newbie to understand if at all that is your concern!! – Eshwar Chaitanya May 24 '13 at 05:38
0

Finally got it working,initially my idea was right,but couldn't execute it properly.First of all we need to fetch the data from txt file and place in array.Then as H2CO3 mentioned we need to loop through and here we need to implement components Separated By String.Now we are ready with data,what needs to be done is placing data in arrays using array object at index 0 in words and 1 in hints,i.e.:

NSString *greFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grewords" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
self.greWordHints = [NSMutableArray array];
self.greWordsArray = [NSMutableArray array];
NSArray *greWords = [NSMutableArray arrayWithArray:[greFileString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
for (NSString *greWord in greWords)
{
   if (greWord && greWord.length)
   {
       NSArray *trimmedGreData = [greWord componentsSeparatedByString:@"@"];
       [greWordsArray addObject:[trimmedGreData objectAtIndex:0]];
       [greWordHints addObject:[trimmedGreData objectAtIndex:1]];
   }
}

Hope it helps some one,thanks :)

Eshwar Chaitanya
  • 942
  • 2
  • 13
  • 34