Array - how can I skip a repeated value in an array?
#1

Hey guys, i hope you are doing alright...
So, I want to know if there's a way to do "continue;" when a value in an array is repeated, im gonna give you an example so you can understand what I'm trying to say:
pawn Код:
{0, 380, "asdfafa", "Other"},
{0, 578, "asdfafafdsafa", "Other"}
{0, 578, "asdfafafdxsafasfa", "Another"}
What I want to do is... By using a loop, extract the last values "Other, Other, Another", And if a value is repeated... skip it...


I want to show this in a dialog, with this code I will get all categories "Other, other ,another" but I just want to get one time the category, It should say: Other,Another.
pawn Код:
for (new x = 0; x < sizeof(datatesting); x++)
{
    format(string, sizeof(string), "%s\t%d\n",datatesting[x][dmCategory],x);
    strcat(fullstr, string);
}
Thanks for reading, I hope you guys help me
Reply
#2

This is the best method I could think of:
pawn Код:
new bool:continueLoop = true;

for (new x = 0; x < sizeof(datatesting); x++)
{
    for (new xx = 0; xx < x; xx++)
    {
        if (strfind(datatesting[xx][dmCategory], datatesting[xx][dmCategory]) == 0) // strfind is faster than strcmp
        {
            continueLoop = false; // we can't break here the main loop, so we set our variable as false
            break; // already found a duplicate, stop this duplicate-detector loop !
        }
    }

    if (continueLoop == false) // if a duplicate one was detected, continue our loop
    {
            continueLoop = true;
            continue;
    }

    format(string, sizeof(string), "%s\t%d\n",datatesting[x][dmCategory],x);
    strcat(fullstr, string);
}
Reply
#3

Quote:
Originally Posted by IstuntmanI
Посмотреть сообщение
This is the best method I could think of:
pawn Код:
new bool:continueLoop = true;

for (new x = 0; x < sizeof(datatesting); x++)
{
    for (new xx = 0; xx < x; xx++)
    {
        if (strfind(datatesting[xx][dmCategory], datatesting[xx][dmCategory]) == 0) // strfind is faster than strcmp
        {
            continueLoop = false; // we can't break here the main loop, so we set our variable as false
            break; // already found a duplicate, stop this duplicate-detector loop !
        }
    }

    if (continueLoop == false) // if a duplicate one was detected, continue our loop
    {
            continueLoop = true;
            continue;
    }

    format(string, sizeof(string), "%s\t%d\n",datatesting[x][dmCategory],x);
    strcat(fullstr, string);
}
Thanks for replying! I just figured a way to do it, too. And that's kinda what I did, thanks a lot
Reply
#4

How about...

Код:
for (new x = 0; x < sizeof(datatesting)-1; x++)
{
	if(strlen(datatesting[x][dmCategory]) < 1) continue;
	for (new xx = x+1; xx < x; xx++)
	{
		if(strlen(datatesting[xx][dmCategory]) < 1) continue;
		if (strfind(datatesting[x][dmCategory], datatesting[xx][dmCategory]) == 0) // strfind is faster than strcmp
		{
			continueLoop = false; // we can't break here the main loop, so we set our variable as false
			break; // already found a duplicate, stop this duplicate-detector loop !
		}
	}

	if (continueLoop == false) // if a duplicate one was detected, continue our loop
	{
			continueLoop = true;
			continue;
	}

	format(string, sizeof(string), "%s\t%d\n",datatesting[x][dmCategory],x);
	strcat(fullstr, string);
}
Reply
#5

Both codes provided above will not work.
Reply
#6

Quote:
Originally Posted by Paulice
Посмотреть сообщение
Both codes provided above will not work.
Hello, the one provided by IstuntmanI works by changing
pawn Код:
if (strfind(datatesting[xx][dmCategory], datatesting[xx][dmCategory]) == 0)
for...
if (strfind(datatesting[x][dmCategory], datatesting[xx][dmCategory]) == 0)
Reply
#7

Quote:
Originally Posted by IstuntmanI
Посмотреть сообщение
This is the best method I could think of:
pawn Код:
new bool:continueLoop = true;

for (new x = 0; x < sizeof(datatesting); x++)
{
    for (new xx = 0; xx < x; xx++)
    {
        if (strfind(datatesting[xx][dmCategory], datatesting[xx][dmCategory]) == 0) // strfind is faster than strcmp
        {
            continueLoop = false; // we can't break here the main loop, so we set our variable as false
            break; // already found a duplicate, stop this duplicate-detector loop !
        }
    }

    if (continueLoop == false) // if a duplicate one was detected, continue our loop
    {
            continueLoop = true;
            continue;
    }

    format(string, sizeof(string), "%s\t%d\n",datatesting[x][dmCategory],x);
    strcat(fullstr, string);
}
Quote:
Originally Posted by Pottus
Посмотреть сообщение
How about...

Код:
for (new x = 0; x < sizeof(datatesting)-1; x++)
{
	if(strlen(datatesting[x][dmCategory]) < 1) continue;
	for (new xx = x+1; xx < x; xx++)
	{
		if(strlen(datatesting[xx][dmCategory]) < 1) continue;
		if (strfind(datatesting[x][dmCategory], datatesting[xx][dmCategory]) == 0) // strfind is faster than strcmp
		{
			continueLoop = false; // we can't break here the main loop, so we set our variable as false
			break; // already found a duplicate, stop this duplicate-detector loop !
		}
	}

	if (continueLoop == false) // if a duplicate one was detected, continue our loop
	{
			continueLoop = true;
			continue;
	}

	format(string, sizeof(string), "%s\t%d\n",datatesting[x][dmCategory],x);
	strcat(fullstr, string);
}
Quote:
Originally Posted by Juand
Посмотреть сообщение
Hello, the one provided by IstuntmanI works by changing
pawn Код:
if (strfind(datatesting[xx][dmCategory], datatesting[xx][dmCategory]) == 0)
for...
if (strfind(datatesting[x][dmCategory], datatesting[xx][dmCategory]) == 0)
using strfind in the place of strcmp is not a good idea as strfind might return 0 for strings like this
PHP код:
"Help me man " and "Help me" 
so both of the code will not work in such conditions.(However pottus' code wont work in any cases (for (new xx = x+1; xx < x; xx++)
Reply
#8

Quote:
Originally Posted by Juand
Посмотреть сообщение
Hello, the one provided by IstuntmanI works by changing
pawn Код:
if (strfind(datatesting[xx][dmCategory], datatesting[xx][dmCategory]) == 0)
for...
if (strfind(datatesting[x][dmCategory], datatesting[xx][dmCategory]) == 0)
Yeah, that's right, forgot to change that.

About the strfind thing, that's right, it just depends on whether he has similar part of strings as categories. If he doesn't have any of that, he should just keep using strfind. Also, about the empty string check: I don't think that you have a category without a name, so that isn't necessary, since you didn't add such a check in the first place anywhere in that loop.

By the way, he solved it already.
Reply
#9

You should loop through found categories instead of all previous categories while working with large arrays (as you mentioned this is for a furniture system? fyi: not on this thread).

Sample:
PHP код:
enum e_array
{
    
something_1,
    
something_2,
    
something_3[40],
    
category[20]
};

new array[][
e_array] =
{
    {
0380"asdfafa""Other"},
    {
0578"asdfafafdsafa""Other"},
    {
0578"asdfafafdxsafasfadwada""Another"},
    {
0578"asdfafafdxsafasfawadaaasd""Another"},
    {
0578"asdfafafdxsafasfawadaaasdfff""Test Something"},
    {
0578"asdfafafdxsafasfawadaaasdffffawfwa""Another Test"},
    {
0578"asdfafafdxsafasfawadaaasd""Test"},
    {
0578"asdfafafdxsafasfawadaaasd""Another"},
    {
0380"asdfafa""Test"}
};

main()
{
    new 
categories[10][20];
    for(new 
0sizeof(array), countbool:duplicatej++, duplicate false)
    {
        for(new 
0categories[k][0] != EOS++)
        {
            if(!
strcmp(array[i][category], categories[k], false))
            {
                
duplicate true;
                break;
            }
        }

        if(!
duplicate)
        {
            
strcat(categories[count ++], array[i][category], 20);

            
printf("%d %s"i, array[i][category]);
        }
    }

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)