0

I have an NSString that returns a list of values like this:

test_1=value_1  
test/2=value_2  
test3=value_3 value_4  
test_4=value_5/value_6  
...  

More realistic result values:

inameX=vlan2    
hname=server    
lanipaddr=192.168.1.1    
lannetmask=255.255.255.0    
islan=0    
islwan=0    
dhcplease=604800    
dhcplease_1=302400  
ct_tcp_timeout=0 1200 40 30 60 60 5 30 15 0
ct_timeout=10 10
ct_udp_timeout=25 60
ctf_disable=1    
ddnsx0=
cifs2=0<\\192.168.1.5
and so on...   

If I do:

 for (id key in dict) {
            NSLog(@"key: %@, value: %@", [dict objectForKey:key], key);
        }

it outputs:

key: inameX, value: vlan2
key: hname value: server    
key: lanipaddr value: 192.168.1.1    
key: lannetmask value: 255.255.255.0 

This list is stored in one NSString *result. Not sure if I should put it in an array for this but I need to be able to call a function or command that will return a specific value_X based on the argument to match the variable. For example, get value of test_1 variable then it would return value_1. Or get test_4 then it would return value_5/value_6

Any idea how I can do that?

I appreciate your help. Thanks!

Squirrel
  • 1,283
  • 1
  • 13
  • 22
  • 1
    Sounds like a homework assignment. Can you be more clear on what you're trying to do and I'll try to lay out the steps on how to accomplish it? What exactly are you inputs and outputs? Are you supposed to parse the string for operators (*/-+)? How do you determine when one test ends and the next begins? – JeffRegan Jul 31 '13 at 03:19
  • Not a homework assignment. I am just trying to learn to interact to a Linux server and get values by executing commands. The output of a specific command would show as I wrote initially to an NSString. I just want to be able to say get value of test_1 so that i can output it to a UILabel value_1 I send NSString *result = [ConnectIt executeIt:TextBox.text]; The result shows as list above. values could be integer or some name – Squirrel Jul 31 '13 at 03:41
  • @JeffCompton any help to parse the list up there would be appreciated. – Squirrel Aug 02 '13 at 02:03

3 Answers3

2

You probably want the method in NSString called componentsSeparatedByCharactersInSet: to split up that one string into an array. Since your values are separated by '=' and new line characters ('\n'), you want the set to include those two characters:

NSArray *strings = [NSString componentsSeparatedByCharactersInSet:
                        [NSCharacterSet characterSetWithCharactersInString:@"=\n"]];

And then you can make this into a dictionary with NSDictoinary's dictionaryWithObjects: AndKeys: But first, you need to split that array into two arrays; one with objects, one with keys:

 NSMutableArray *keys = [NSMutableArray new];
    NSMutableArray *values = [NSMutableArray new];
    for (int i = 0; i < strings.count; i++) {
        if (i % 2 == 0) { // if i is even
            [keys addObject:strings[i]];
        }
        else {
            [values addObject:strings[i]];
        }
    }

Then you put them into an NSDictonary

NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"%@", dict[@"test_1"])  // This should print out 'value_1'

Hope that helps!

Kenny Bambridge
  • 407
  • 4
  • 14
  • You are right, I overlooked which object that method uses, fixed that. However I was creating an NSSet with an object, `@['=','\n']` is an `NSArray` object. – Kenny Bambridge Jul 31 '13 at 03:41
  • This doesn't work. I get bad exec. `NSString *result = [ConnectIt executeIt:TextBox.text];` `NSArray *strings = [result componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"=\n"]];` `NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:strings];` `NSLog(@"%@", dict[@"test_1"]);` – Squirrel Jul 31 '13 at 04:11
  • The last edit should work for you. I forgot you can't put an array in `dictionaryWithObjectsAndKeys:`. – Kenny Bambridge Jul 31 '13 at 04:26
  • I ran once and it gave me the right value based on dict[@"test_1"]. I changed that to dict[@"test_2"] but it output (null). Any idea? – Squirrel Jul 31 '13 at 23:46
  • Sample list output from result: `inameX=vlan2 lanipaddr=192.168.1.1 islan=0 dhcplease=604800` I want to be able to say get me the value of inameX then output is vlan2 If I ask for dhcplease then output is 604800 – Squirrel Aug 01 '13 at 00:17
  • So it looks like you need another delimeter, if you are separating things with spaces then add a space to `@"=\n"` – Kenny Bambridge Aug 01 '13 at 03:10
  • Sorry that is a list but i dont know how to make is so here. each parameter=value in a separate line. Also, i could have dhcplease=604800 and dhcplease_1=302400. I think in these cases it outputs null – Squirrel Aug 01 '13 at 03:15
  • From the latest edit above's output. How do I get only specific object's and its value from that? Say I want to know the value of dhcplease_1. – Squirrel Aug 01 '13 at 04:18
  • Not sure if anyone can help me because I staill can't output all values. It seems that for loop doesn't catch all values and keys. – Squirrel Aug 01 '13 at 23:06
0

Use an NSDicationary. NSDictionaries are key value stores. In other words, there are a list of keys. Each key is unique. Each key has an associated value. The value can be any data type and the key has to conform to the NSCopying protocol (typically an NSString). If you try to access the value for a key that doesn't exist in your NSDictionary, the return value will be nil.

//create the dictionary and populate it
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"value_1" forKey:@"key_1"];
[dict setObject:@"value_2" forKey:@"key_2"];
[dict setObject:@"value_3" forKey:@"key_3"];
[dict setObject:@"value_4" forKey:@"key_4"];


NSString *stringInput = [self getStringInput];//however you find out your input

//find your string value based on the key passed in
NSString *strValue = [dict objectForKey:stringInput];
JeffRegan
  • 1,322
  • 9
  • 25
  • I could use that but these values changes dynamically meaning test_1 would have value_1 now but value_8 in 5 minutes from now if I reload the data. Basically everytime I reload NSString *result it will have a list with the same variables but with different values. – Squirrel Jul 31 '13 at 03:12
  • @Squirrel `NSMutableDictionary`... I don't see why an array is better for implementing mutable dictionaries. –  Jul 31 '13 at 03:46
  • I think you mean `setObject:forKey:`, not `setValue:forKey:`, and the key doesn't have to be an NSString; it just has to conform to the NSCopying protocol. – Aaron Brager Jul 31 '13 at 03:51
  • @AaronBrager `setValue:forKey:` is just fine. –  Jul 31 '13 at 04:45
  • I was curious what the difference was. For anyone else that is also curious: http://stackoverflow.com/questions/1249634/wheres-the-difference-between-setobjectforkey-and-setvalueforkey-in-nsmutab – JeffRegan Jul 31 '13 at 04:50
  • @JeffCompton The difference is an `@` sign :) –  Jul 31 '13 at 07:33
0

You can use a NSScanner to make this work.

Scan for the string for which you want the value, and then scan the string until you encounter \n and then use it for your requirement.

NSScanner *scan =[NSScanner scannerWithString:theString];
[scan scanString:keyString inToString:nil];
[scan setScanLocation:[scan scanLocation]+1];

[scan scanString:@"\n" inToString:requiredString];

So requiredString is the string which you want.