SRL Archive - Research & Development Lounge

Dg's Small Procedures


Dgby714

2010-10-13 03:12:11

When doing personal projects I tend too make some useful functions and procedures.
So I'm gonna post them all here =)
Some of these might already be in SCAR/Simba/SRL (I didn't see it?)

{*******************************************************************************
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
By: Dgby714, Smartzkid, Sandstorm
Description: Removes the Index'th item from Arr
*******************************************************************************}
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
var
I, P: integer;
begin
if (not (InRange(Index, Low(Arr), High(Arr)))) then
Exit;
P := High(Arr);
for I := Index + 1 to P do
Arr[I - 1] := Arr[I];
SetArrayLength(Arr, Length(Arr) - 1);
end;

{*******************************************************************************
function InArr(const Data, Arr: variant): boolean;
By: Dgby714
Description: Returns True if Data is a item in Arr.
*******************************************************************************}
function InArr(const Data, Arr: variant): boolean;
var
I, P, T: integer;
begin
if (VarType(Arr) = 8204) then
begin
P := High(TVariantArray(Arr));
T := VarType(Data);
for I := 0 to P do
if (T = VarType(Arr[I])) then
if (Data = Arr[I]) then
begin
Result := True;
Exit;
end;
end;
end;

{*******************************************************************************
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 := (Pos(Lowercase(Text), Lowercase(Data)) > 0);
end;

{*******************************************************************************
procedure QScreenShot;
By: Dgby714
Description: Saves a screenshot into your AppPath/Screenshots
*******************************************************************************}
procedure QScreenShot;
var
Path, Filename: string;
I: integer;
begin
Path := AppPath + 'Screenshots' + {$IFDEF WINDOWS}'\'{$ELSE}'/'{$ENDIF};
if (not (DirectoryExists(Path))) then
if (not (CreateDirectory(Path))) then
RaiseException(erCustomError, 'Exception in QScreenShot: Cannot create directory "' + Path + '"');
for I := 0 to High(I) do
begin
Filename := 'Screenshot_' + IntToStr(I) + '.bmp';
if (not (FileExists(Path + Filename))) then
Break;
end;
SaveScreenshot(Path + Filename);
DebugLn('QScreenShot: Screenshot saved as "' + Path + Filename + '"');
end;

{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714, Sandstorm, Smartzkid
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
var
I: integer;
begin
Result := '';
for I := 1 to Len do
Result := Result + chr(33 + Random(93));
end;

{*******************************************************************************
function Center(str: string; len: integer): string;
By: Dgby714
Description: Returns a str centered with len Length [Center('hey', 5) = ' hey ']
*******************************************************************************}
function Center(str: string; len: integer): string;
begin
Result := Trim(str);
while (Length(Result) < Len) do
begin
if (Length(Result) = (Len - 1)) then
Result := Result + ' '
else
Result := ' ' + Result + ' ';
end;
end;

{*******************************************************************************
function TimeStamp: string;
By: Dgby714
Description: Returns the current time in 24 hour HH:MM:SS format
*******************************************************************************}
function TimeStamp: string;
var
Time: TDateTime;
H, M, S, MS: word;
begin
Time := Now;
DecodeTime(Time, H, M, S, MS);
Result := Padz(IntToStr(H), 2) + ':' + Padz(IntToStr(M), 2) + ':' + Padz(IntToStr(S), 2);
end;

{*******************************************************************************
procedure WaitEx(TimeInMS: integer);
By: Dgby714
Description: Waits TimeInMs + Rand(10%) unless 1000 > TimeInMS > 5000!
*******************************************************************************}
procedure WaitEx(TimeInMS: integer);
var
RandTime: integer;
begin
if (TimeInMS < 1000) then
RandTime := Random(1000)
else if (TimeInMS > 5000) then
RandTime := Random(5000)
else
RandTime := Random(TimeInMS / 10);
Wait(TimeInMS + RandTime);
end;

{*******************************************************************************
procedure WriteINIArr(Section, Name: string; Arr: TStringArray; Filename: string);
By: Dgby714
Description: Writes Arr to Filename under Scetion (The items are named Name)
*******************************************************************************}
procedure WriteINIArr(Section, Name: string; Arr: TStringArray; Filename: string);
var
I: integer;
begin
WriteINI(Section, 'Name', Name, Filename);
WriteINI(Section, 'Count', IntToStr(Length(Arr)), Filename);
for I := Low(Arr) to High(Arr) do
WriteINI(Section, Name + '[' + IntToStr(I) + ']', Arr[I], Filename);
end;

{*******************************************************************************
function ReadINIArr(Section, Filename: string): TStringArray;
By: Dgby714
Description: Returns a array from Filename under Section (Use WriteINIArr first)
*******************************************************************************}
function ReadINIArr(Section, Filename: string): TStringArray;
var
Name: string;
Count, I: integer;
begin
Name := ReadINI(Section, 'Name', Filename);
Count := StrToIntDef(ReadINI(Section, 'Count', Filename), 0);
SetArrayLength(Result, Count);
for I := 0 to Count - 1 do
Result[I] := ReadINI(Section, Name + '[' + IntToStr(I) + ']', Filename);
end;

{*******************************************************************************
function ToExtended(Data: variant): extended;
By: Dgby714
Description: Returns Data as a Double/Extended
*******************************************************************************}
function ToExtended(Data: variant): extended;
begin
case VarType(Data) of
varInteger, varInt64, varSmallInt, varShortInt, varWord, varLongWord, varByte: Result := Data * 1.0;
varString, varOleStr: Result := StrToIntDef(Data, 0) * 1.0;
varNull: Result := 0.0;
varDouble: Result := Data;
varBoolean: if (Data) then
Result := 1.0
else
Result := 0.0;
end;
end;


These next ones are some I've found aren't needed =/ Ill give why too.

{*******************************************************************************
function Space(num: integer): string;
By: Dgby714
Description: Returns num spaces
*******************************************************************************}
function Space(num: integer): string;
var
I: Integer;
begin
Result := '';
for I := 0 to Num do
Result := Result + ' ';
end;
This one can be….
StringOfChar(' ', 5);

Dgby714

2010-10-13 03:13:14

Some useful integer array procedures.
function MostCommonNumber(Arr: TIntegerArray): LongInt;
var
H, I, Cur, Count, HighCount: LongInt;
begin
QuickSort(Arr);

Cur := Arr[0] - 1;
Count := 0;
HighCount := 0;

H := High(Arr);
for I := 0 to H do
begin
if (Cur <> Arr[I]) then
begin
if (Count > HighCount) then
begin
HighCount := Count;
Result := Cur;
end;

Count := 0;
Cur := Arr[I];
end;

Inc(Count);
end;
end;

{*******************************************************************************
procedure Push(var Arr: TIntegerArray; Val: integer);
By: Dgby714
Description: Pushes Val onto Arr.
*******************************************************************************}
procedure Push(var Arr: TIntegerArray; Val: integer);
var
L: integer;
begin
L := Length(Arr);
SetLength(Arr, L + 1);
Arr[L] := Val;
end;

{*******************************************************************************
function Pop(var Arr: TIntegerArray): integer;
By: Dgby714
Description: Pops a val off of Arr.
*******************************************************************************}
function Pop(var Arr: TIntegerArray): integer;
var
H: integer;
begin
H := High(Arr);
Result := Arr[H];
SetLength(Arr, H);
end;

{*******************************************************************************
function PopI(var Arr: TIntegerArray; Index: integer): integer;
By: Dgby714
Description: Pops val at Index off of Arr.
*******************************************************************************}
function PopI(var Arr: TIntegerArray; Index: integer): integer;
var
H, I: integer;
begin
H := High(Arr);
Result := Arr[Index];
for I := Index + 1 to H do
Arr[I - 1] := Arr[I];
SetLength(Arr, H);
end;

{*******************************************************************************
function RandIntArr(L: integer): TIntegerArray;
By: Dgby714
Description: Creates a randomly sorted Integer array from 0 to L. Simple Solution.
*******************************************************************************}
function RandIntArr(L: integer): TIntegerArray;
var
I, J, H: integer;
IntArr: TIntegerArray;
begin
SetLength(IntArr, L);
for I := 0 to L - 1 do
IntArr[I] := I;

while (Length(IntArr) > 0) do
begin
SetLength(Result, Length(Result) + 1);
J := Random(High(IntArr) + 1);
Result[High(Result)] := IntArr[J];

H := High(IntArr);
for I := J + 1 to H do
IntArr[I - 1] := IntArr[I];
SetLength(IntArr, Length(IntArr) - 1);
end;
end;

{*******************************************************************************
function RandIntArr(L: integer): TIntegerArray;
By: Dgby714
Description: Creates a randomly sorted Integer array from 0 to L. Elegant Solution.
*******************************************************************************}
function RandIntArr(L: integer): TIntegerArray;
var
I, J, H, C: integer;
RArr: TPointArray;
begin
SetLength(RArr, 1);
RArr[0] := Point(0, L - 1);

SetLength(Result, L);
C := 0;

while (Length(RArr) > 0) do
begin
J := Random(Length(RArr));
Result[C] := RandomRange(RArr[J].X, RArr[J].Y);

if (not ((Result[C] = RArr[J].X) or (Result[C] = RArr[J].Y))) then
begin
SetLength(RArr, Length(RArr) + 1);
RArr[High(RArr)] := Point(Result[C] + 1, RArr[J].Y);
RArr[J].Y := Result[C] - 1;
end else
begin
if ((Result[C] = RArr[J].X) and (Result[C] = RArr[J].Y)) then
begin
H := High(RArr);
for I := J + 1 to H do
RArr[I - 1] := RArr[I];
SetLength(RArr, Length(RArr) - 1);

Inc(C);
Continue;
end;

if (Result[C] = RArr[J].X) then
Inc(RArr[J].X);

if (Result[C] = RArr[J].Y) then
Dec(RArr[J].Y);
end;

Inc(C);
end;
end;

{*******************************************************************************
function DownloadToFile(const URL, Filename: string): boolean;
By: Dgby714
Description: Downloading file from URL to Filename.
*******************************************************************************}
function DownloadToFile(const URL, Filename: string): boolean;
var
FileI: LongInt;
FileC, FileH: string;
begin
Result := (not (FileExists(Filename)));
if (Result) then
begin
Result := False;
FileI := InitializeHTTPClient(True);
try
FileC := GetHTTPPage(FileI, URL);
FileH := GetRawHeaders(FileI);

if ((FileC = '') or (FileH = '')) then
begin
WriteLn('Error downloading "' + URL + '".');
Exit;
end;

FileH := Copy(GetRawHeaders(FileI), 10, Pos(#13, FileH) - 10); //Error Code
if (FileH <> '200 OK') then
begin
WriteLn('Couldn''t download "' + URL + '", Error Code: ' + FileH + '.');
Exit;
end;
finally
FreeHTTPClient(FileI);
end;

FileI := CreateFile(Filename);
try
Result := WriteFileString(FileI, FileC);
finally
CloseFile(FileI);
end;
end;
end;

function IntToStrC(const Num: LongInt): string; //IntToStr with commas
begin
Result := Format('%M', [Extended(Num)]);
Result := Copy(Result, 2, Length(Result) - 4);
end;

procedure CopyIntArr(const Src: TIntegerArray; out Dest: TIntegerArray);
var
I, H: LongInt;
begin
H := Length(Src);
SetLength(Dest, H);
Dec(H);
for I := 0 to H do
Dest[I] := Src[I];
end;

procedure Copy2DIntArr(const Src: T2DIntegerArray; out Dest: T2DIntegerArray);
var
I, H: LongInt;
begin
H := Length(Src);
SetLength(Dest, H);
Dec(H);
for I := 0 to H do
CopyIntArr(Src[I], Dest[I]);
end;


//FloatToStr that handles filtering letters out... 'l33t' = 33
function GetNumbers(const x: string): Extended;
begin
Result := StrToFloatDef(ReplaceRegExpr('[^\d+-\.]*([-+]?[0-9]*\.?[0-9]+)[^\d]*', x, '$1', True), 0);
end;

Dgby714

2010-10-13 03:14:52

Not exactly a procedure/function, but @Probably Glitched; needed help with this so I made it after seeing @bob_dole;'s version (here).

program MinimapBuilder;
{$DEFINE SMART}
{$I SRL-OSR/SRL.Simba}

const
Width = 4;
Height = 2;
FileName = 'Minimaps/minimap1.bmp';

function getCircle(Radius: LongInt; Center: TPoint): TPointArray;
var
RadiusSqrd: Extended;
X, Y, I: LongInt;
begin
RadiusSqrd := Sqr(Radius);
SetLength(Result, Floor(Pi * RadiusSqrd));

for X := -Radius to Radius do
for Y := -Radius to Radius do
if ((Sqr(X) + Sqr(Y)) <= RadiusSqrd) then
begin
Result[I] := Point(X + Center.X, Y + Center.Y);
Inc(I);
end;

SetLength(Result, I);
end;

procedure WaitForSmart(Enable: Boolean);
begin
repeat
Wait(10);
until (Enable = SmartEnabled(SmartCurrentTarget));
end;

var
Circle: TPointArray;
Bitmap, I: LongInt;
Colors: TIntegerArray;

begin
Circle := getCircle(66, Point(642, 76));

SetupSRL();
Bitmap := CreateBitmap(Width * 132 + 1, Height * 132 + 1);

for I := 0 to Width * Height - 1 do
begin
WaitForSmart(True);

WriteLn('Saving map #' + IntToStr(I) + '... ');
try
Colors := GetColors(Circle);
FastSetPixels(Bitmap, getCircle(66, Point(132 * (I mod Width) + 66, 132 * Floor(I div Width) + 66)), Colors);
except
WriteLn('Error Saving?');
Continue;
end;
WriteLn('Saved!');

WaitForSmart(False);
end;

SaveBitmap(Bitmap, FileName);

DrawBitmapDebugImg(Bitmap);
DisplayDebugImgWindow(Width * 132, Height * 132);

FreeBitmap(Bitmap);
end.

Sex

2010-10-13 04:13:12

Why is this in tutorials?

Dgby714

2010-10-13 13:10:05

From: Sex
Why is this in tutorials?


Where else should i of put it?

Cstrike

2010-10-13 13:37:47

Cool, good job.
I'm going to find this one useful soon

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

Rich

2010-10-13 13:39:53

From: Dgby714
Where else should i of put it?

Here.

Dgby714

2010-10-13 13:44:28

From: Rich
Here.


I don't want them added too SRL though? I was just posting them so other could use them.

Rich

2010-10-13 13:49:30

From: Dgby714
I don't want them added too SRL though? I was just posting them so other could use them.


Here then.
Test Corner Post your procedures and functions here

Dgby714

2010-10-13 13:53:09

From: Rich
Here then.


Isn't that for testing?

Well if a mod+ see this they can move it there^

Sandstorm

2010-10-13 21:45:21

{*******************************************************************************
function Center(str: string; len: integer): string;
By: Dgby714
Description: Returns a str centered with len Length [Center('hey', 5) = ' hey ']
*******************************************************************************}
function Center(str: string; len: integer): string;
begin
Result := Trim(str);
while (Length(Result) < Len) do
begin
if (Length(Result) = (Len - 1)) then
begin
Result := Result + ' ';
end else
begin
Result := ' ' + Result + ' ';
end;
end;
end;

If syntax is the same as SCAR:

{*******************************************************************************
function Center(str: string; len: integer): string;
By: Dgby714
Description: Returns a str centered with len Length [Center('hey', 5) = ' hey ']
*******************************************************************************}
function Center(str: string; len: integer): string;
begin
Result := Trim(str);
while (Length(Result) < Len) do
begin
if (Length(Result) = (Len - 1)) then
Result := Result + ' '
else
Result := ' ' + Result + ' ';
end;
end;;


{*******************************************************************************
function TimeStamp: string;
By: Dgby714
Description: Returns the current time in 24 hour HH:MM:SS format
*******************************************************************************}
function TimeStamp: string;
var
H, M, S, MS: word;
begin
DecodeTime(Now, H, M, S, MS);
Result := Padz(IntToStr(H), 2) + ':' + Padz(IntToStr(M), 2) + ':' + Padz(IntToStr(S), 2);
end;

Not sure if that second one is properly done, but it cuts out an unneeded variable.

{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
begin
while (not (Length(Result) >= Len)) do
begin
Result := Result + Chr(RandomRange(33, 125))
end;
end;

Just some shortenings. Don't ask me why :|.

Dgby714

2010-10-13 22:14:24

From: Sandstorm
{*******************************************************************************
function Center(str: string; len: integer): string;
By: Dgby714
Description: Returns a str centered with len Length [Center('hey', 5) = ' hey ']
*******************************************************************************}
function Center(str: string; len: integer): string;
begin
Result := Trim(str);
while (Length(Result) < Len) do
begin
if (Length(Result) = (Len - 1)) then
Result := Result + ' '
else
Result := ' ' + Result + ' ';
end;
end;;


{*******************************************************************************
function TimeStamp: string;
By: Dgby714
Description: Returns the current time in 24 hour HH:MM:SS format
*******************************************************************************}
function TimeStamp: string;
var
H, M, S, MS: word;
begin
DecodeTime(Now, H, M, S, MS);
Result := Padz(IntToStr(H), 2) + ':' + Padz(IntToStr(M), 2) + ':' + Padz(IntToStr(S), 2);
end;

{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
begin
while (not (Length(Result) >= Len)) do
begin
Result := Result + Chr(RandomRange(33, 125))
end;
end;


Yeah idk why i added those begin/ends

I got a type mismatch when using Now directly in DecodeTime…

I made RandomKey before i knew about Chr()

Sandstorm

2010-10-13 22:16:16

I personally don't have Simba installed, and will probably be sticking to SCAR, so those DO compile in SCAR, and I am completely clueless about what will compile in Simba.

Dgby714

2010-10-13 22:20:29

They should all compile in Simba too

Added some functions i just made for my IRC bot =)

i luffs yeww

2010-10-13 22:23:22


{*******************************************************************************
function Center(str: string; len: integer): string;
By: Dgby714
Description: Returns a str centered with len Length [Center('hey', 5) = ' hey ']
*******************************************************************************}
function Center(str: string; len: integer): string;
begin
Result := Trim(str);
while (Length(Result) < Len) do
if (Length(Result) = (Len - 1)) then
Result := Result + ' '
else
Result := ' ' + Result + ' ';
end;

:3

Looks nice btw.

Smartzkid

2010-10-13 22:33:04

{*******************************************************************************
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
By: Dgby714
Description: Removes the Index'th item from Arr
*******************************************************************************}
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
var
I: integer;
begin
if (not (InRange(Index, Low(Arr), High(Arr)))) then
Exit;

for I := Index + 1 to High(Arr) do
Arr[I - 1] := Arr[I];
SetArrayLength(Arr, Length(Arr) - 1);
end;

I believe that would do the same thing a bit quicker. Could this be changed to Array of Variant so it would work for any array?

{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
var
cTemp: char;
begin
Result := '';
while (not (Length(Result) >= Len)) do
begin
cTemp := chr(33 + Random(93));
Result := Result + cTemp;
end;
end;

Ought to be a great deal quicker. If you want to optimize it even more, use a for loop.

Sandstorm

2010-10-13 22:41:56

{*******************************************************************************
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
By: Dgby714
Description: Removes the Index'th item from Arr
*******************************************************************************}
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
var
I, P : integer;
begin
if (not (InRange(Index, Low(Arr), High(Arr)))) then
Exit;
P := High(Arr);
for I := Index + 1 to P do
Arr[I - 1] := Arr[I];
SetArrayLength(Arr, Length(Arr) - 1);
end;

Would, I believe, be even faster. Doesn't have to do High(Arr) every time it loops.

Dgby714

2010-10-14 12:27:42

From: Smartzkid
I believe that would do the same thing a bit quicker. Could this be changed to Array of Variant so it would work for any array?


I dont thing the TVariantArray works the same as a Variant. If we changed it you wouldn't be able to pass a TStringArray as the parameter, or any other array. It would be looking for a TVariantArray.

Ex:
program new;

procedure Test(TestArr: TVariantArray);
begin
end;

var
TestArr2: TStringArray;

begin
Test(TestArr2);
end.

Results in
Error: Type Mismatch at line 11

Smartzkid

2010-10-14 19:20:02

From: Sandstorm
{*******************************************************************************
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
By: Dgby714
Description: Removes the Index'th item from Arr
*******************************************************************************}
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
var
I, P : integer;
begin
if (not (InRange(Index, Low(Arr), High(Arr)))) then
Exit;
P := High(Arr);
for I := Index + 1 to P do
Arr[I - 1] := Arr[I];
SetArrayLength(Arr, Length(Arr) - 1);
end;

Would, I believe, be even faster. Doesn't have to do High(Arr) every time it loops.


Yup!

From: Dgby714
I dont thing the TVariantArray works the same as a Variant. If we changed it you wouldn't be able to pass a TStringArray as the parameter, or any other array. It would be looking for a TVariantArray.


I see.


program new;

procedure Test(TestArr: variant);
var
i: integer;
begin
for i := 0 to 9 do
writeln(testarr[i]);
end;

var
TestArr2: TStringArray;
i: integer;

begin
setlength(testarr2, 10);
for i := 0 to 9 do
testarr2[i] := inttostr(i);
Test(TestArr2);
end.

That works, but when you declare the variant as a var, it gives trouble. Sadly, I don't think there is a solution.

Dgby714

2010-10-14 20:31:47

From: Smartzkid
…


Hmm Make more procedures instead like a RemoveIntArrIndex?

Smartzkid

2010-10-14 20:33:25

Or get someone to write it for simba, in which case it could work like writeln

Dgby714

2010-10-17 12:09:36

From: Smartzkid
Or get someone to write it for simba, in which case it could work like writeln


That wont work… But i think we could make it
function RemoveArrIndex(Index: integer; Arr: Variant): Variant;
Altho we would need to change the guts a little…

Dgby714

2010-12-25 12:52:16

From: Smartzkid

{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
var
cTemp: char;
begin
Result := '';
while (not (Length(Result) >= Len)) do
begin
cTemp := chr(33 + Random(93));
Result := Result + cTemp;
end;
end;

Ought to be a great deal quicker. If you want to optimize it even more, use a for loop.


Wouldn't this be faster?
{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714, Sandstorm, Smartzkid
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
var
I: integer;
begin
SetLength(Result, Len);
for I := 1 to Len do
Result[I] := chr(33 + Random(93));
end;

Blumblebee

2010-12-25 16:50:50

From: Dgby714
Wouldn't this be faster?
{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714, Sandstorm, Smartzkid
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
var
I: integer;
begin
SetLength(Result, Len);
for I := 1 to Len do
Result[I] := chr(33 + Random(93));
end;


Are you sure that would work? You deal with the string like it's an array which I didn't think was possible…

i luffs yeww

2010-12-25 18:29:19

It would work if you imploded Result after, but that'd just make it longer for no reason (since you can't use Implode unless you change each item in the array to a string).


{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714, Sandstorm, Smartzkid
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: Integer): string;
var
i: Integer;
begin
for i := 0 to Len - 1 do
Result := Result + chr(33 + Random(93));
end;

That works nicely.

Frement

2010-12-25 18:33:29

From: Blumblebee
Are you sure that would work? You deal with the string like it's an array which I didn't think was possible…


Why wouldn't it work? After all, a string is just an array of chars.

i luffs yeww

2010-12-25 18:44:38

But in practical use, it interprets it as a literal array of chars, and not as a string, so it gives you a type mismatch.

Frement

2010-12-25 18:51:27

From: i luffs yeww
But in practical use, it interprets it as a literal array of chars, and not as a string, so it gives you a type mismatch.


program new;

function RandomKey(Len: integer): string;
var
I: integer;
begin
SetLength(Result, Len);
for I := 1 to Len do
Result[I] := chr(33 + Random(93));
end;

begin
Writeln(RandomKey(5));
end.

Compiled succesfully in 15 ms.
Wf|#!
Successfully executed.

Works for me.

i luffs yeww

2010-12-25 18:52:46

wat

Swore it gave me an error before..

Either way, mine was a single line shorter. :3

Blumblebee

2010-12-25 19:01:06

cool, you learn something new everyday haha

Smartzkid

2010-12-26 18:00:27

From: i luffs yeww
wat

Swore it gave me an error before..

Either way, mine was a single line shorter. :3


hopes that pascal strings are mutable ;)

Dgby714

2010-12-26 18:15:18

From: i luffs yeww
wat

Swore it gave me an error before..

Either way, mine was a single line shorter. :3


Nope Try this

program new;

{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714, Sandstorm, Smartzkid
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: Integer): string;
var
i: Integer;
begin
for i := 0 to Len - 1 do
Result := Result + chr(33 + Random(93));
end;

var
Test: string;

begin
Test := 'Dgby714 rocks * '
Test := RandomKey(12);
WriteLn(Test);
end.

Output:
Compiled succesfully in 16 ms.
Dgby714 rocks * qDpmw:/pFMmJ
Successfully executed.

i luffs yeww

2010-12-26 18:40:40

;) I know.

It makes sense, I'm just confus on why I thought it didn't work before.

BraK

2011-05-29 20:30:57

Moved as requested/also part of the cleaning up of tutorial Island.

~BraK

Dgby714

2011-05-29 21:01:31

From: BraK
Moved as requested/also part of the cleaning up of tutorial Island.

~BraK


Thanks =) New Section? Didn't see it before.

Gonna be adding more to it soon.

loragnor

2013-06-25 01:37:50

I know this is a very old thread, but @Dgby, have you put all these funcs into a single file so they can be used as an include?