Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to use gmatch in objective c?

Hey, I was looking around in codea and found gmatch very interesting and was winding how I could do it in objective c


-- Generate table from string t = {} s = "from=world, to=Lua" for k, v in string.gmatch(s,"(%w+)=(%w+)") do -- t[k] = v print(k .. "-" .. v) end

I wanna know how I do do that in objective c with the exact same result

Comments

  • dave1707dave1707 Mod
    Posts: 7,554

    @_Tommy I don’t know anything about objective c, but a google search came up with "rangeOfString" to find a string within another string. I don’t know if there’s any parameters that can be used with "rangeOfString” to make it like gmatch. Either you can look for more info on that, or wait for someone who knows objective c to give you a better answer.

  • edited March 2018 Posts: 1
    #include <stdio.h>
    #include <string.h>
    
    int match(char [], char[]);
    
    int main() {
      char a[100], b[100];
      int position;
    
      printf("Enter some text\n");
      gets(a);
    
      printf("Enter a string to find\n");
      gets(b);
    
      position = match(a, b);
    
      if(position != -1) {
        printf("Found at location %d\n", position + 1);
      }
      else {
        printf("Not found.\n");
      }
    
      return 0;
    }
    
    int match(char text[], char pattern[]) {
      int c, d, e, text_length, pattern_length, position = -1;
    
      text_length    = strlen(text);
      pattern_length = strlen(pattern);
    
      if (pattern_length > text_length) {
        return -1;
      }
    
      for (c = 0; c <= text_length - pattern_length; c++) {
        position = e = c;
    
        for (d = 0; d < pattern_length; d++) {
          if (pattern[d] == text[e]) {
            e++;
          }
          else {
            break;
          }
        }
        if (d == pattern_length) {
          return position;
        }
      }
    
      return -1;
    }
    
  • Posts: 2,161

    This is a forum for discussion about Codea, Objective-C is off topic. You would be better served asking your question on Stackoverflow.

    @hitachisupport Consider this a warning. I have no idea if the code you posted is relevant or where it came from, but keep your posts on topic and do not include links to irrelevant websites. Such behaviour is considered spamming and will result in you being banned.

    I am closing this discussion as it is off-topic for this forum.

This discussion has been closed.