Help Removing duplicates value from array

utofaery

New member
Messages
101
Points
0
Code:
    getinventorylist();
    for ( .@i = 0; .@i < @inventorylist_count; .@i++ ) {
        setarray .@IdList[.@i],@inventorylist_id[.@i];
    }
Q1

For above code how would I remove duplicates item ID or extract only Unique Item ID within array of .@idList so the end result would be array with no duplicated item id in it?

=================================================================================================================================================

Peace dividing line!

=================================================================================================================================================

Code:
[Error]: buildin_countitem: Invalid item '0'.
[Debug]: Source (NPC): Mass Junk Seller at prontera (164,175)
[Error]: buildin_countitem: Invalid item '0'.
[Debug]: Source (NPC): Mass Junk Seller at prontera (164,175)
[Error]: buildin_countitem: Invalid item '0'.
[Debug]: Source (NPC): Mass Junk Seller at prontera (164,175)
Q2

Above Error always happens whenever this part of function runs what can be done with it ?

but server and client runs fine.

Function \\// Down here:

Code:
function    script    F_SellEquips    {
    setarray(.@noSell0x[0], 1231, 969, 4001 );
    getinventorylist();
    for ( set .@i,0; .@i < @inventorylist_count; set .@i,.@i + 1) {
        if ( ( getiteminfo(@inventorylist_id[.@i],ITEMINFO_TYPE) == IT_WEAPON ) || ( getiteminfo(@inventorylist_id[.@i],ITEMINFO_TYPE) == IT_ARMOR ) ) && ( @inventorylist_equip[.@i] == 0 ) && ( @inventorylist_refine[.@i] == 0 ) && ( @inventorylist_card1[.@i] == 0 ) && ( @inventorylist_card2[.@i] == 0 ) && ( @inventorylist_card3[.@i] == 0 ) && ( @inventorylist_card4[.@i] == 0 ) {
            .@FinishCheck = 0;
            for( .@c = 0; .@c <  getarraysize(.@noSell0x); ++.@c ) {
                if (  @inventorylist_id[.@i] == .@noSell0x[.@c] ) { // Yes to sell
                    .@FinishCheck++;
                }
            }
            if ( .@FinishCheck == 0 ) {
                setarray .@sellid[getarraysize(.@sellid)],@inventorylist_id[.@i];
            }
        }
    }
    .@sellarraysize = getarraysize(.@sellid);
    .@xTItemCount = 0;
    .@xTItemZeny = 0;
    .@over = getskilllv("MC_OVERCHARGE");
    if(.@over > 0)    
        .@plus = 5 + 2*.@over - .@over/10;
    message strcharinfo(0),( " Sell array size " + .@sellarraysize );
    if ( .@sellarraysize > 0) {
        .@i = 0;
        while (.@i <= .@sellarraysize) {
            .@SitemID = .@sellid[.@i];
            if ( ( .@sellid[.@i] != 0 ) && ( .@sellid[.@i] > 0 ) && ( countitem(.@sellid[.@i] ) > 0 ) ) {
                .@SitemName$ = getitemname(.@sellid[.@i]);
                .@SitemCount = countitem(.@sellid[.@i]);
                .@SitemPrice = ( getiteminfo(.@sellid[.@i],1) * (100 + .@plus) / 100 ) ;
                .@xTItemZeny = .@xTItemZeny + ( .@SitemPrice * .@SitemCount ) ;
                .@xTItemCount = .@xTItemCount + .@SitemCount;
                dispbottom ("ItemCount :: " + .@SitemCount + "  ItemPrice :: " + .@SitemPrice + " ItemID :: " + .@sellid[.@i] + "  ItemName :: " + .@SitemName$ );
                delitem2 (.@SitemID,.@SitemCount,true,0,0,0,0,0,0);
            }
            ++.@i;
        }
        Zeny = Zeny + .@xTItemZeny ;
        dispbottom ( " Total Zeny Gained :: " + .@xTItemZeny + "  for total item sold :: " + .@xTItemCount ) ;
    }
    if ( .@sellarraysize == 0 ) {
        dispbottom ( " Nah ! no item can be sale, you liar! " );
    }
    close2;
    end;
}

 
Last edited by a moderator:
see array_unique() in




 
Let me show you 3 different methods

1.  loop the value back in another array, almost similar to meko did

Code:
prontera,155,185,5	script	kjdshfsk	1_F_MARIA,{
	getinventorylist;
	for ( .@i = 0; .@i < @inventorylist_count; ++.@i ) {
		.@j = 0;
		while ( .@j < .@itemtotal && .@itemid[.@j] != @inventorylist_id[.@i] ) ++.@j;
		if ( .@j == .@itemtotal )
			.@itemid[.@itemtotal++] = @inventorylist_id[.@i];
	}
	for ( .@i = 0; .@i < .@itemtotal; ++.@i )
		dispbottom getitemname( .@itemid[.@i] ) +" -> "+ countitem( .@itemid[.@i] ) +"x";
	end;
}
most people will show you this method, and this method is usable in almost all programming language
BUT ... in my opinion this method use lots of loops ...

2. store the value in a string, then compare them later

Code:
prontera,158,185,5	script	dskjfhsdfk	1_F_MARIA,{
	getinventorylist;
	.@compare$ = "#";
	for ( .@i = 0; .@i < @inventorylist_count; ++.@i ) {
		if ( !compare( .@compare$, "#"+ @inventorylist_id[.@i] +"#" ) ) {
			.@compare$ += @inventorylist_id[.@i] +"#";
			.@itemid[.@itemtotal++] = @inventorylist_id[.@i];
		}
	}
	for ( .@i = 0; .@i < .@itemtotal; ++.@i )
		dispbottom getitemname( .@itemid[.@i] ) +" -> "+ countitem( .@itemid[.@i] ) +"x";
	end;
}
I have used this method in
https://rathena.org/board/topic/91826-special-party-warper/#comment-241434
https://rathena.org/board/topic/91723-please-help-this-script-about-mac_address/?do=findComment&comment=240887
I used this method a lot before Ind upgrade our scripting engine,
but search using strings is quite slow in C language, hercules script language included
and comes the recommended method below

3. abuse hercules script engine, array is store in a pointer.

Code:
prontera,161,185,5	script	zcxvsfer	1_F_MARIA,{
	getinventorylist;
	for ( .@i = 0; .@i < @inventorylist_count; ++.@i ) {
		if ( !.@compare[ @inventorylist_id[.@i] ] ) {
			.@compare[ @inventorylist_id[.@i] ] = true;
			.@itemid[.@itemtotal++] = @inventorylist_id[.@i];
		}
	}
	for ( .@i = 0; .@i < .@itemtotal; ++.@i )
		dispbottom getitemname( .@itemid[.@i] ) +" -> "+ countitem( .@itemid[.@i] ) +"x";
	end;
}
ever since Ind upgrade our scripting engine, this is my latest method, and I think this is the fastest way -> compare to all 3 methods
I have used in
getitemname2 function
soul linker spirit

As you can see, I used Method 2 while still on rAthena forum, and switch to Method 3 after switch to Hercules

And for your 2nd question, you can solve it yourself after you learn any of these techniques

 
Last edited by a moderator:
Nice Case Closed

Annie thanks for showing all this stuff 

Guess will go the 3rd options.

since using hercules as main rathena as supportive testing only..

 
Back
Top