Results 1 to 9 of 9

Thread: Counting same items in a array

  1. #1
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Counting same items in a array

    Hi Guys/Girls,

    Searched the internet and only found how to count number of items in array.

    what i am after is to return how many of each item is in array.

    Array:= [1, 2, 2, 3, 4, 5, 2, 2, 3]

    number of 1's = 1
    number of 2's = 4
    number of 3's = 2
    number of 4's = 1
    number of 5's = 1

    it should return how many there is.

    Thanks

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

    Default

    SCAR Code:
    var
      i, c: Integer;
    begin
      c := 0;
      for i := 0 to High(array) do
        if (array[i] = num) then
          c := c + 1;
    end;

    If you wanted something that returns the count of all numbers, then peculiarly TPointArray's are well suited - you can store the number it responds to in the y then hold the count of that number using x. So, get the unique numbers, assign them to the .y values in the TPA then loop through all numbers and increase the .x of the .y with the same .y as the number.
    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.

  3. #3
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    ok cool,

    does this also work for strings? its what i am going to be using i simply used numbers as an example

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

    Default

    sure, though they will be case sensitive, so you may want to use Lowercase or Uppercase to ensure that same string but different case are counted the same, ie
    SCAR Code:
    if (Lowercase(myStrArray[i]) = 'wat') then
    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.

  5. #5
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    so i need a if statement for each string in my array's.

    my array contains item i do not know, as i check the text in runescape and add players to it, do i simply get the result in my first array slot and check it agains the rest, then after i searched all of the array move onto result in second slot and search again.

    edit: Also how do i set up the TPointArray ?

    so i can store player name in .x and the count of it in .y?

    i supose i can check the name in array agains the excisting TPointArray's and if its not there simply add it.

    If anyone can show me how to setup a TPointArray with .x and .y values would be cool.
    Last edited by Bobzilla69; 09-08-2009 at 11:22 PM.

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

    Default

    Well, I was only using a TPA tally as a suggestion as you were using numbers - for strings, just make your own record of a string and an integer (for the tally) to make it easier to keep track of them, like so:

    SCAR Code:
    type
      TStrTally = record
        s: string;
        c: Integer;
      end;

    function TallyStrs(strs: TStringArray): array of TStrTally;
    var
      t, i, n, h: Integer;
      unStrs: TStringArray;
    begin
      h := High(strs);
      SetLength(unStrs, h + 1);
      n := -1;
      for i := 0 to h do
      begin
        for t := 0 to n do
        begin
          if (unStrs[t] = strs[i]) then
            break;
        end;
        if (t = n + 1) then
        begin
          n := t;
          unStrs[n] := strs[i];
        end;
      end;
     
      SetLength(Result, n + 1);
     
      for t := 0 to n do
      begin
        Result[t].s := unStrs[t];
        Result[t].c := 0;
        for i := 0 to h do
        begin
          if (Result[t].s = strs[i]) then
            Result[t].c := Result[t].c + 1;
        end;
      end;
     
    end;

    var
      t: array of TStrTally;
      i: Integer;
    begin
      Writeln('Begin');
      t := TallyStrs(['hey', 'wat', 'hey', 'there', 'wat', 'hiya', 'hey']);
      for i := 0 to High(t) do
        Writeln(t[i].s + ' :' + IntToStr(t[i].c));
      Writeln('End');
    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.

  7. #7
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    i am a bit confused by your reply, would it be possible to have a few annotations to help me understand whats going on.

    i kinda follow it but not all of it.

    thanks

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

    Default

    Can be a bit confusing, commented
    SCAR Code:
    type
      TStrTally = record
        s: string;
        c: Integer;
      end;

    function TallyStrs(strs: TStringArray): array of TStrTally;
    var
      t, i, n, h: Integer;
      unStrs: TStringArray;
    begin
      h := High(strs); // high of all strings
      SetLength(unStrs, h + 1); // Used to store unique strings
      n := -1; // used as the current high for the unique strings
      for i := 0 to h do // go through all the strings
      begin
        for t := 0 to n do // go through all unique strings
        begin
          if (unStrs[t] = strs[i]) then // if this string matches a unique string
            break; // then break as it is not unique
        end;
        if (t = n + 1) then // if the for loop did not break
        begin
          n := t; // increase the unique string high
          unStrs[n] := strs[i]; // and assigned the string as a new unique string
        end;
      end;

      SetLength(Result, n + 1); // As n is the high, n + 1 is the length used

      for t := 0 to n do // loop through all unique strings
      begin
        Result[t].s := unStrs[t]; // Assign the string to the tally
        Result[t].c := 0; // Reset the count
        for i := 0 to h do // loop through all the strings
        begin
          if (Result[t].s = strs[i]) then // if the strings match
            Result[t].c := Result[t].c + 1; // then increase the count
        end;
      end;
    end;

    var
      t: array of TStrTally;
      i: Integer;
    begin
      Writeln('Begin');
      t := TallyStrs(['hey', 'wat', 'hey', 'there', 'wat', 'hiya', 'hey']);
      for i := 0 to High(t) do
        Writeln(t[i].s + ' :' + IntToStr(t[i].c));
      Writeln('End');
    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.

  9. #9
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    ok thanks that helped alot.

    how would i take a String array and make it a TStrTally?

    i am trying to do it by looping through my array and adding them

    this is my code
    SCAR Code:
    for z:=0 to High(PeopleTalkingArray) do //get high of my array of players
      begin
        SetLength(t, Length(t) + 1); //increase length of t by one
        t[High(t)]:= PeopleTalkingArray[z]; //adds first result from people array by using z
        WriteLn('Person in Array : '+PeopleTalkingArray[Z]+'Place in array : ' +IntToStr(z)) //used as debug to show whats happening
      end;

    the problem i get is it says PeopleTalkingArray[z], is type miss match
    any ideas

    Edit: All that was needed to annd my array to the t, so that it will work with your function was

    SCAR Code:
    t:= TallyStrs(PeopleTalkingArray);

    thanks for all your help mixster
    Last edited by Bobzilla69; 09-09-2009 at 12:47 AM.

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
  •