Results 1 to 18 of 18

Thread: Organizing An Array Help

  1. #1
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default Organizing An Array Help

    Ok, how could i organize an array so certain items are first.

    Example :
    SCAR Code:
    TypesofPie := ['apple','banana','cherry','poison'];
    TypesofPie := setitemstofirst(['poison','banana'],TypesofPie ); // Function setitemstofirst(itemstofirst, the array: TStringArray): TStringArray;

    That would make the array: "poison,banana,apple,cherry".
    I do visit every 2-6 months

  2. #2
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    By making a function ?

    I'll make one right now, give me a few minutes.

    Edit: There you go..
    SCAR Code:
    function SetItemsToFirst(Strings, Arr : TStringArray) : TStringArray;
    var
      i, ii, h : integer;
    begin
      h := high(Arr);
      for i := 0 to h do
        if InStrArr(Arr[i], Strings, false) then
        begin
          Swap(Arr[ii], Arr[i]);
          inc(ii);
        end;
    end;

    Haven't scripted in a while, so it could be better/shorter haha. But it works.
    Last edited by Sex; 10-31-2009 at 12:18 AM.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  3. #3
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    program New;
    var
    kill : TStringArray;

    function SetItemsToFirst(Strings, Arr : TStringArray) : TStringArray;
    var
      i, ii, h : integer;
    begin
      h := high(Arr);
      for i := 0 to h do
        if InStrArr(Arr[i], Strings, false) then
        begin
          Swap(Arr[ii], Arr[i]);
          inc(ii);
        end;
    end;
    begin
       kill := ['cat','pod','beer','love'];
       kill := SetItemsToFirst(['pod','love'],kill);
       writeln(kill[2]);
    end.

    with your function should be cat, but its beer
    I do visit every 2-6 months

  4. #4
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    So you want me to reserve the order of the array and the only difference being that the strings you choose in the SetItemsToFirst is in the front?
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  5. #5
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    yes.
    I do visit every 2-6 months

  6. #6
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Okay. How about you PM me your MSN and get online and we will talk.
    Last edited by Sex; 10-31-2009 at 12:56 AM.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  7. #7
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    I do visit every 2-6 months

  8. #8
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    SCAR Code:
    function SetItemsFirst(items, arr: TStringArray): Tstringarray;
    var
      i, j, k, h, itemLength: Integer;
    begin
      // Initialize all variables.
      h := High(arr);
      SetLength(Result, h + 1);
      k := High(items);
      itemLength := k + 1;
      for i := 0 to h do
        // if the String is in the items array, we add it to the first index available, held by j
        if InStrArr(arr[i], items, false) then
        begin
          Result[j] := arr[i];
          Inc(j);
        end else
        // Otherwise, we add it to the array as normal. This will keep the order minus the ones extracted.
        begin
          Result[k] := arr[i];
          Inc(k);
        end;
    end;

    Test it.. I think it would work..

    This WILL messup if any of the items AREN'T in the array. I think..
    Last edited by Nava2; 10-31-2009 at 01:04 AM.
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  9. #9
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Nope, it doesn't Nava. Just tested.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  10. #10
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    SCAR Code:
    function SetItemsFirst(items, arr: TStringArray): Tstringarray;
    var
      i, j, k, h, itemLength, arrPos: Integer;
    begin
      // Initialize all variables.
      h := High(arr);
      SetLength(Result, h + 1);
      k := High(items);
      itemLength := k + 1;
      for i := 0 to h do
        // if the String is in the items array, we add it to the first index available, held by j
        if InStrArrEx(arr[i], items, false, arrPos) then
          Result[arrPos] := arr[i];
        else
        // Otherwise, we add it to the array as normal. This will keep the order minus the ones extracted.
        begin
          Result[k] := arr[i];
          Inc(k);
        end;
    end;

    Try that.
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  11. #11
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    No go nava2..

    Line 17: [Error] (20597:35): Variable Expected in script C:\Users\zasz\Desktop\sdf.scar

    Plox help.
    I do visit every 2-6 months

  12. #12
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    The parameters in this line are wrong:
    SCAR Code:
    if InStrArrEx(arr[i], items, false, arrPos) then

    Here it is from SRL:
    SCAR Code:
    {*******************************************************************************
    Function InStrArrEx(S: String; StrArr: TStringArray; Var Where: Integer): Boolean;
    By: n3ss3s
    Description: Returns true if the string S was found in the StrArr, and if so
    stores the index into Where.
    *******************************************************************************}

    Function InStrArrEx(S: String; StrArr: TStringArray; Var Where: Integer): Boolean;
    Var
       I, H: Integer;
    Begin
      H := High(StrArr);
      For I := 0 To H Do
        If LowerCase(StrArr[i]) = LowerCase(S) Then
        Begin
          Where := i;
          Result := True;
          Break;
        End;
    End;

    Unless I'm missing something.

  13. #13
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    SCAR Code:
    function SetItemsFirst(items, arr: TStringArray): Tstringarray;
    var
      i, j, k, h, itemLength, arrPos: Integer;
    begin
      // Initialize all variables.
      h := High(arr);
      SetLength(Result, h + 1);
      k := High(items);
      itemLength := k + 1;
      for i := 0 to h do
        // if the String is in the items array, we add it to the first index available, held by j
        if InStrArrEx(arr[i], items, arrPos) then
          Result[arrPos] := arr[i]
        else
        // Otherwise, we add it to the array as normal. This will keep the order minus the ones extracted.
        begin
          Result[k] := arr[i];
          Inc(k);
        end;
    end;
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  14. #14
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    After messing with SEX's code.

    I figured it out, quite easily.
    SCAR Code:
    program New;
    var
    kill : TStringArray;
    me : Integer;

    function SetItemsToFirst(Strings, Arr : TStringArray) : TStringArray;
    var
      i, ii : integer;
    begin
      for i := 0 to high(Arr) do
        if InStrArr(Arr[i], Strings, false) then
        begin
          Swap(Arr[ii], Arr[i]);
          Swap(Arr[i],Arr[ii+2]);
          inc(ii);
        end;
    end;


    begin
    cleardebug;
       kill := ['cat','pod','beer','love'];
       kill := SetItemsToFirst(['pod','love'],kill);
       for me := 0 to high(kill)do
       writeln(kill[me]);
    end.

    will return "Pod, love, cat, beer."
    I do visit every 2-6 months

  15. #15
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by ZaSz View Post
    After messing with SEX's code.

    I figured it out, quite easily.
    SCAR Code:
    program New;
    var
    kill : TStringArray;
    me : Integer;

    function SetItemsToFirst(Strings, Arr : TStringArray) : TStringArray;
    var
      i, ii : integer;
    begin
      for i := 0 to high(Arr) do
        if InStrArr(Arr[i], Strings, false) then
        begin
          Swap(Arr[ii], Arr[i]);
          Swap(Arr[i],Arr[ii+2]);
          inc(ii);
        end;
    end;


    begin
    cleardebug;
       kill := ['cat','pod','beer','love'];
       kill := SetItemsToFirst(['pod','love'],kill);
       for me := 0 to high(kill)do
       writeln(kill[me]);
    end.

    will return "Pod, love, cat, beer."
    Yours won't order the "first" items.

    E: It also returns nothing in the result, nor in a variable.. :P
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  16. #16
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Yeah, i realized, it organizes the "first" items by the order in the original array :/ How can i fix that?
    I do visit every 2-6 months

  17. #17
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Assuming unique input only, I would suggest looping through the order first array. If it's found, then add it to the result and store the index. After looping through them, loop through the original input and skip the stored indexes (ie those matched). Would probablybe faster in Scar to just do the found check again though.

    Semi-pseudocode would resemble something like
    SCAR Code:
    function SortStringsFirst(start, input: TStringArray):TStringArray;
    begin
      SetLength(Result, input);
      for i := 0 to start do
        if InStrArr(start[i], input) then
          Result[x] := start[i];
          x := x + 1;
     
      for i := 0 to input do
        if not InStrArr(input[i], start) then
          Result[x] := input[i];
          x := x + 1;
    end;
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  18. #18
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    I had a very short one yesterday but I lost it and am too lazy to redo it but here is a rough one I just did:
    SCAR Code:
    function SetItemsFirst(Items, Input : TStringArray) : TStringArray;
    var
      i, ii : integer;
      h, hh : integer;
      TIA : TIntegerArray;
    begin
      hh := high(Input);
      for i := 0 to hh do
        if not InStrArr(Input[i], Items, False) then
        begin
          h := high(TIA);
          SetArrayLength(TIA, h + 2);
          TIA[h + 1] := i;
        end;
      for i := 0 to high(Input) do
        if not InIntArray(TIA, i) then
        begin
          SetArrayLength(Result, GetArrayLength(Result) + 1);
          Result[High(Result)] := Input[i];
        end;
      for i := 0 to h + 1 do
      begin
        SetArrayLength(Result, GetArrayLength(Result) + 1);
        Result[High(Result)] := Input[TIA[i]];
      end;
    end;
    It is not that good of code though. If you want me to refine it I will .

    But I can confirm that it does work.
    Last edited by Sex; 11-01-2009 at 06:19 PM.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

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
  •