Jump to content
  • 0
utofaery

Help Removing duplicates value from array

Question

    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!

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

[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:

Spoiler

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;
}

 

 

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Let me show you 3 different methods

 

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

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

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&amp;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.

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

 

Edited by AnnieRuru

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.