Results 1 to 12 of 12

Thread: Writing strings into an array

  1. #1
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default Writing strings into an array

    I'm writing a function which splits a string into all the possible sequential counterparts, eg the string "box" has the possibility of "b", "o", "x", "bo", "ox" and "box". So all characters must be touching.

    I've got a function which (in my head) should work theoretically, but doesn't when ran, displaying many blank strings, and 1 or 2 repeated sections of the string. If I'm right, the error is where the section of string is actually written to the array.

    The code:
    Simba Code:
    program StringSplit;
    var
    string1: string;
    stringArray: Array of String;

    function SplitString(str: string): Array of String;
    var
    i, n, possibles: integer;
    begin
      n := Length(str);
      for i := Length(str) downto 0 do
        possibles := possibles + n;
      SetArrayLength(Result, possibles - 1);
      for i := 0 to Length(str) - 1 do //for each position in the string
        for n := 0 to Length(str) - 1 do //for each possible length of the string
          Result[i+n] := Copy(str, i, n); //writes string part into result array
      for i := 0 to high(Result) do
        writeLN(Result[i]);
    end;

    begin
      stringArray := SplitString('string');
      string1 := stringArray[random(High(stringArray))];
      WriteLN(string1);
    end.

    (this will have a practical use eventually, and hopefully won't be as inefficient as I think it will...)

    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Is this what you wanted?

    Simba Code:
    program StringSplit;

    function SplitString(str: string): TStringArray;
    var
      L, I, K: integer;
    begin
      L := Length(str);
      K := 0;
      SetArrayLength(Result, L * 3 - 3);
      for I := 1 to L do
      begin
        Result[K] := str[I];
        if (I + 1 <= L) then
        begin
          Inc(K);
          Result[K] := str[I] + str[I + 1];
          if (I - 1 > 0 ) then
          begin
            Inc(K);
            Result[K] := str[I - 1] + str[I] + str[I + 1];
          end;
        end;
        Inc(K);
      end;
    end;

    begin
      WriteLn(SplitString('dgrocks?'));
    end.

    Output:
    Code:
    ['d', 'dg', 'g', 'gr', 'dgr', 'r', 'ro', 'gro', 'o', 'oc', 'roc', 'c', 'ck', 'ock', 'k', 'ks', 'cks', 's', 's?', 'ks?', '?']
    Last edited by Dgby714; 12-17-2010 at 04:46 AM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  3. #3
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    That's not all the possibilities, Dg. What about "dgro" or "dgroc" or "dgrock" or "dgrocks" or "dgrocks?" etc. There are a lot of possible combinations with this.

  4. #4
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by i luffs yeww View Post
    That's not all the possibilities, Dg. What about "dgro" or "dgroc" or "dgrock" or "dgrocks" or "dgrocks?" etc. There are a lot of possible combinations with this.
    Ah.... Hm.... I got mislead from the box example =( Ill go back to the drawing board...

    Edit: Ok I've got this.... Tho I don't like how I got the Length of Result I think there is a better way.

    Simba Code:
    program StringSplit;

    function SplitString(str: string): TStringArray;
    var
      L, K, I, J, H, M: integer;
      SA: TStringArray;
    begin
      str := Trim(str);
      L := Length(str);
      K := 0;
      J := 0;
      SA := Explode(' ', str);
      for I := Low(SA) to High(SA) do
        for M := 0 to Length(SA[I]) - 1 do
          IncEx(J, Length(SA[I]) - M);
      SetArrayLength(Result, J);
      for I := 1 to L do
      begin
        if (str[I] = ' ') then
          Continue;
        Result[K] := str[I];
        for J := I + 1 to L do
        begin
          if (str[J] = ' ') then
            break;
          Inc(K);
          Result[K] := '';
          for H := I to J do
            Result[K] := Result[K] + str[H]
        end;
        Inc(K);
      end;
    end;

    begin
      WriteLn(SplitString('Dgby714 Rocks!'));
    end.

    Output:
    Code:
    ['D', 'Dg', 'Dgb', 'Dgby', 'Dgby7', 'Dgby71', 'Dgby714', 'g', 'gb', 'gby', 'gby7', 'gby71', 'gby714', 'b', 'by', 'by7', 'by71', 'by714', 'y', 'y7', 'y71', 'y714', '7', '71', '714', '1', '14', '4', 'R', 'Ro', 'Roc', 'Rock', 'Rocks', 'Rocks!', 'o', 'oc', 'ock', 'ocks', 'ocks!', 'c', 'ck', 'cks', 'cks!', 'k', 'ks', 'ks!', 's', 's!', '!']
    Last edited by Dgby714; 12-17-2010 at 03:16 PM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  5. #5
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    (this will have a practical use eventually, and hopefully won't be as inefficient as I think it will...)
    With my code above^
    Simba Code:
    begin
      try
        F := OpenFile(ScriptPath + 'splitstring.simba', True);
        try
          ReadFileString(F, S, FileSize(F))
          t := GetSystemTime;
          WriteLn(SplitString(S));
          WriteLn('Took ' + IntToStr(GetSystemTime - t) + ' ms.');
        finally
          CloseFile(F);
        end;
      except
        WriteLn('Hmmm.. I couldnt open splitstring.simba.');
      end;
    end.

    Code:
    Compiled succesfully in 15 ms.
    ['p', ... '.']
    Took 655 ms.
    Successfully executed.
    Hehe.... The array was too long... Got this from making this reply...
    Code:
    The text that you have entered is too long (112859 characters). Please shorten it to 100000 characters long.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  6. #6
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by Dgby714 View Post
    With my code above^
    Simba Code:
    begin
      try
        F := OpenFile(ScriptPath + 'splitstring.simba', True);
        try
          ReadFileString(F, S, FileSize(F))
          t := GetSystemTime;
          WriteLn(SplitString(S));
          WriteLn('Took ' + IntToStr(GetSystemTime - t) + ' ms.');
        finally
          CloseFile(F);
        end;
      except
        WriteLn('Hmmm.. I couldnt open splitstring.simba.');
      end;
    end.

    Code:
    Compiled succesfully in 15 ms.
    ['p', ... '.']
    Took 655 ms.
    Successfully executed.
    Hehe.... The array was too long... Got this from making this reply...
    Code:
    The text that you have entered is too long (112859 characters). Please shorten it to 100000 characters long.
    What on earth was the string? :P Thanks, makes sense how you did it, I'll credit you in the script I make with it. I was trying to make an InStr function, but we'll see about the efficieny...

  7. #7
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    What on earth was the string? :P Thanks, makes sense how you did it, I'll credit you in the script I make with it. I was trying to make an InStr function, but we'll see about the efficieny...
    He loaded a script into SliptString()
    No wonder the array was too long

  8. #8
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Oh, didn't read the code properly :P
    I did a test with a fairly long string without spaces:

    Simba Code:
    begin
      t := GetSystemTime
      WriteLn(SplitString('thisisaverylongstringwithoutanyspaces,meaningthisshouldtakeawhile.surprisingly,thisdoesn''actuallytakeforevertorun'));
      WriteLN(IntToStr(GetSystemTime - t)+'ms');
    end.

    and got 1904ms, so... faster than I thought!

  9. #9
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Simba Code:
    function RandomString(Len : Integer) : String;
    var
      I : Integer;
    begin
      for I := 1 to Len do Result := Result + Chr(RandomRange(VK_A, VK_Z));
    end;

    var
      t : Integer;

    begin
      t := GetSystemTime;
      SplitString(RandomString(512));
      WriteLn(GetSystemTime - t);
    end.
    Took 201,117ms

  10. #10
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    What on earth was the string? :P Thanks, makes sense how you did it, I'll credit you in the script I make with it. I was trying to make an InStr function, but we'll see about the efficieny...
    InStr?

    Simba Code:
    {*******************************************************************************
    function in_string(Text, Data: string): boolean;
    By: Dgby714
    Description: Returns True if Text is in Data
    *******************************************************************************}

    function in_string(Text, Data: string): boolean;
    begin
      Result := (PosEx(Lowercase(Text), Lowercase(Data), 1) > 0);
    end;

    Like that? ^

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  11. #11
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by Dgby714 View Post
    InStr?

    Simba Code:
    {*******************************************************************************
    function in_string(Text, Data: string): boolean;
    By: Dgby714
    Description: Returns True if Text is in Data
    *******************************************************************************}

    function in_string(Text, Data: string): boolean;
    begin
      Result := (PosEx(Lowercase(Text), Lowercase(Data), 1) > 0);
    end;

    Like that? ^
    Exactly like that :L Didn't realise we had one of those, when I control spaced, I only searched for InStr (I probably hadn't even included SRL). Ah well, I still have uses for this, and a couple of other string functions I've got.

  12. #12
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    Exactly like that :L Didn't realise we had one of those, when I control spaced, I only searched for InStr (I probably hadn't even included SRL). Ah well, I still have uses for this, and a couple of other string functions I've got.
    =) idk if its in SRL I made that function awhile back when I needed it and posted it in the tuts section.

    Link: http://villavu.com/forum/showthread.php?t=59475

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •