SRL Archive - OSR Help

Writing strings into an array


Richard

2010-12-16 18:29:21

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:

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

Dgby714

2010-12-16 23:43:40

Is this what you wanted?

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:
['d', 'dg', 'g', 'gr', 'dgr', 'r', 'ro', 'gro', 'o', 'oc', 'roc', 'c', 'ck', 'ock', 'k', 'ks', 'cks', 's', 's?', 'ks?', '?']

i luffs yeww

2010-12-17 02:27:15

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.

Dgby714

2010-12-17 09:20:46

From: i luffs yeww
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.

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:
['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!', '!']

Dgby714

2010-12-17 10:26:51

From: Richard

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


With my code above^
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.

Compiled succesfully in 15 ms.
['p', ... '.']
Took 655 ms.
Successfully executed.

Hehe…. The array was too long… Got this from making this reply…
The text that you have entered is too long (112859 characters). Please shorten it to 100000 characters long.

Richard

2010-12-17 13:17:27

From: Dgby714
With my code above^
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.

Compiled succesfully in 15 ms.
['p', ... '.']
Took 655 ms.
Successfully executed.

Hehe…. The array was too long… Got this from making this reply…
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…

Gu3st...

2010-12-17 17:09:11

From: Richard
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() :p
No wonder the array was too long :redface:

Richard

2010-12-17 17:24:46

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

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

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

Gu3st...

2010-12-17 18:44:14

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 :p

Dgby714

2010-12-17 19:39:01

From: Richard
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?

{*******************************************************************************
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? ^

Richard

2010-12-17 19:54:17

From: Dgby714
InStr?

{*******************************************************************************
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.

Dgby714

2010-12-17 21:30:14

From: Richard
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