How to remove Property from Array?

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.

Moderator: POL Developer

Post Reply
Harley
Forum Regular
Posts: 360
Joined: Sat Mar 18, 2006 1:41 am
Location: Germany

How to remove Property from Array?

Post by Harley »

Hi there)

I wrote that code:

Code: Select all

	var buffs := GetObjProperty( who, "#Buffs" );
	if( !buffs )
		buffs := { };
	endif

	if( !( icon in buffs ) )
		buffs.Append( icon );
		SetObjProperty( who, "#Buffs", buffs );
	endif
After buff, I have 6 icons (int types) in #Buff property.
Now I need to remove one of them. How should I do this?
I think I tired, 'cause don't understand what to write)

Thanks for helping!
Best regards!
User avatar
CWO
POL Expert
Posts: 1158
Joined: Sat Feb 04, 2006 5:49 pm
Location: Chicago, IL USA

Re: How to remove Property from Array?

Post by CWO »

It's been YEARS since I scripted POL so there may be a new way but

If you're removing one buff, you can do a foreach and erase the buff directly from the array. This ONLY works if you're removing one buff at a time because after erasing, it will throw the loop off sync with the array position:

Code: Select all

foreach item in buffs
     if (item == icon)
          buffs.erase(_item_iter);
          break;
      endif
endforeach
SetObjProperty( who, "#Buffs", buffs );
if you need to remove multiple buffs, you need to make a new array and append everything that isn't matching what you're removing. Something like this:

Code: Select all

var newbuffs := [];
foreach item in buffs
   if ( !(item == icon)
        newbuffs.append(item);
        continue;
    endif
endforeach
SetObjProperty( who, "#Buffs", newbuffs );
Duttones
Apprentice Poster
Posts: 54
Joined: Tue Mar 27, 2012 8:56 pm

Re: How to remove Property from Array?

Post by Duttones »

I created a lib to use with arrays for POL.

If someone found it interesting.

You can search for values inside structs too, something like:
myarray := array{struct{"id":=1, "color":=23}}
elem_found := FindElemInArray(myarray, struct{"id":=1});

And it removes things from array with the function RemoveFromArray

Code: Select all

/** $Id: arrays.inc 1433 2006-05-18 21:48:04Z austin $
 *
 * Purpose
 * Provide various functions for array handling.
 *
 */
use basic;
use math;
use os;
use uo;

/*
 * GetArrayRange(the_array, start, stop)
 *
 * Purpose
 * Retrieves a range from the array.
 *
 * Parameters
 * the_array:	The array to extract data from.
 * start:	Start index to get data from.
 * stop:	End index to get data from.
 *		If stop is 0, will go until the end of the array is reached.
 *
 * Return value
 * Returns an array on success.
 * Returns an error on failure.
 *
 */
function GetArrayRange(byref the_array, start, stop:=0)
	if ( Lower(TypeOf(the_array)) != "array" )
		return error{"errortext":="Object passed was not an array."};
	endif

    start := CInt(start);
    stop  := CInt(stop);

	if ( start < 1 )
		start := 1;
	endif
	if ( !stop )
		stop := the_array.Size();
	elseif ( stop > the_array.Size() )
		stop := the_array.Size();
	endif

	var new_array := array{};
	for ( start; start<=stop; start:=start+1 )
		new_array.Append(the_array[start]);
		sleepms(2);
	endfor

	return new_array;
endfunction

/*
 * BreakArray(the_array, size)
 *
 * Purpose
 * Breaks an array up into smaller arrays.
 *
 * Parameters
 * the_array:	The array that will be broken up.
 * size:	Size of the new arrays.
 *
 * Return value
 * Returns an array of arrays
 *
 */
function BreakArray(byref the_array, size:=1)
	if ( Lower(TypeOf(the_array)) != "array" )
		return error{"errortext":="Object passed was not an array."};
	endif

	if ( the_array.Size() > size )
		var new_arrays := array();
		var arrays_needed := Ceil(CDbl(the_array.Size()) / CDbl(size));

		var i := 1;
		var start := i;
		var stop := size;
		for ( i; i<=arrays_needed; i:=i+1 )
			new_arrays.Append(GetArrayRange(the_array, start, stop));
			start := stop+1;
			stop := stop+size;
			sleepms(2);
		endfor

		return new_arrays;
	else
		return array{the_array};
	endif
endfunction

/*
 * FindInArray(byref the_array, find_what)
 *
 * Purpose
 * Finds the first instance of something in an array.
 *
 * Parameters
 * the_array:	Array to search.
 * find_what:	What to look for.
 *
 * Return value
 * Returns > 1 (the index) if it is found.
 * Returns 0 if 'find_what' was not found.
 *
 */
function FindInArray(byref the_array, find_what)
	if ( Lower(TypeOf(the_array)) != "array" )
		return error{"errortext":="Object passed was not an array."};
	endif

	foreach index in ( the_array )
        if ((TypeOf(find_what) == "Struct" || TypeOf(index) == "Struct") || (TypeOf(index) == "Dictionary" || TypeOf(find_what) == "Dictionary"))
            var can_return := array{};
            foreach key in (find_what.Keys())
                if (index.Exists(key) && find_what[key] == index[key])
                    can_return.append(1);
                else
                    can_return.append(2);
                endif
                sleepms(2);
            endforeach

            if (!(2 in can_return))
                return _index_iter;
            endif
		elseif ( index == find_what )
			return _index_iter;
		endif
		sleepms(2);
	endforeach

	return 0;
endfunction

/*
 * FindElemInArray(byref the_array, find_what)
 *
 * Purpose
 * Finds the first instance of something in an array.
 *
 * Parameters
 * the_array:   Array to search.
 * find_what:   What to look for.
 *
 * Return value
 * Returns the elem of what its searching
 * Returns 0 if 'find_what' was not found.
 *
 */
function FindElemInArray(byref the_array, find_what)
    if ( Lower(TypeOf(the_array)) != "array" )
        return error{"errortext":="Object passed was not an array."};
    endif

    foreach index in ( the_array )
        if ((TypeOf(find_what) == "Struct" || TypeOf(index) == "Struct") || (TypeOf(index) == "Dictionary" || TypeOf(find_what) == "Dictionary"))
            var can_return := array{};
            foreach key in (find_what.Keys())
                if (index.Exists(key) && find_what[key] == index[key])
                    can_return.append(1);
                else
                    can_return.append(2);
                endif
                sleepms(2);
            endforeach

            if (!(2 in can_return))
                return index;
            endif
        elseif ( index == find_what )
			return index;
		endif
        sleepms(2);
    endforeach

    return 0;
endfunction

/*
 * FindAllElemInArray(byref the_array, find_what)
 *
 * Purpose
 * Finds the all instances of something in an array.
 *
 * Parameters
 * the_array:   Array to search.
 * find_what:   What to look for.
 *
 * Return value
 * Returns all elem of what its searching
 * Returns 0 if 'find_what' was not found.
 *
 */
function FindAllElemInArray(byref the_array, find_what)
    if ( Lower(TypeOf(the_array)) != "array" )
        return error{"errortext":="Object passed was not an array."};
    endif

    var to_return := array{};
    foreach index in ( the_array )
        if ((TypeOf(find_what) == "Struct" || TypeOf(index) == "Struct") || (TypeOf(index) == "Dictionary" || TypeOf(find_what) == "Dictionary"))
            foreach key in (find_what.Keys())
                if (index.Exists(key) && find_what[key] == index[key])
                    to_return.append(index);
                endif
                sleepms(2);
            endforeach
        elseif ( index == find_what )
			return to_return.append(index);
        endif
        sleepms(2);
    endforeach

	if (to_return.size() >= 1)
		return to_return;
	endif
	
    return 0;
endfunction


/*
 * RemoveFromArray(byref the_array, delete_what)
 *
 * Purpose
 * Remove any element mathing delete_what from the array.
 *
 * Parameters
 * the_array:	Array to search.
 * delete_what:	What to look for removing. Can pass an array to remove multiple elems.
 *
 * Return value
 * Returns the array without the elems
 *
 */
function RemoveFromArray(the_array, delete_what)
	if ( Lower(TypeOf(delete_what)) != "array")
		delete_what := array{delete_what};
	endif
	
	var iter_to_remove := array{};
	foreach item in (the_array)
		if (item in delete_what)
			iter_to_remove += _item_iter;
		endif
		sleepms(5);
	endforeach

	iter_to_remove.Reverse();
	foreach iter in iter_to_remove
		the_array.Erase(iter);
	endforeach

	return the_array;
endfunction

/*
 * RemoveLowest(byref the_array)
 *
 * Purpose
 * Remove lowest integer or real value from array
 *
 * Parameters
 * the_array:	Array to search.
 *
 * Return value
 * Returns the array without the lowest value
 *
 */
function RemoveLowest(the_array)
	if (the_array.Size() <= 1)
        return the_array;
	endif

	var i;
	var l_val := 1;
	for (i := 1; i < the_array.Size(); i+=1)
		if (the_array[l_val] > the_array[i])
			l_val := i;
		endif
	endfor

	the_array.Erase(l_val);
	return the_array;
endfunction

/*
 * RemoveHighest(byref the_array)
 *
 * Purpose
 * Remove lowest integer or real value from array
 *
 * Parameters
 * the_array:	Array to search.
 *
 * Return value
 * Returns the array without the highest value
 *
 */
function RemoveHighest(the_array)
	if (the_array.Size() <= 1)
        return the_array;
	endif

	var i;
	var l_val := 1;
	for (i := 1; i < the_array.Size(); i+=1)
		if (the_array[l_val] < the_array[i])
			l_val := i;
		endif
	endfor

	the_array.Erase(l_val);
	return the_array;
endfunction

/*
 * ArraySum(byref the_array)
 *
 * Purpose
 * Sum all integer/real values inside the array.
 *
 * Parameters
 * the_array:	Array to sum.
 *
 * Return value
 * Returns the value of sum of the values of the array
 *
 */
function ArraySum(the_array)
	if (the_array.Size() <= 1)
        return the_array[1];
	endif

	var f_val := 0;
	foreach entry in (the_array)
		f_val += entry;
	endforeach

	return f_val;
endfunction
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: How to remove Property from Array?

Post by Turley »

I would simply write the following:
var buffs:={1,2,3};
var id:=2;
var index := id in buffs;
if (index)
buffs.erase(index);
endif
print(buffs);
Harley
Forum Regular
Posts: 360
Joined: Sat Mar 18, 2006 1:41 am
Location: Germany

Re: How to remove Property from Array?

Post by Harley »

Duttones wrote: Tue Aug 08, 2017 4:41 am I created a lib to use with arrays for POL.

If someone found it interesting.

You can search for values inside structs too, something like:
myarray := array{struct{"id":=1, "color":=23}}
elem_found := FindElemInArray(myarray, struct{"id":=1});

And it removes things from array with the function RemoveFromArray

Code: Select all

/** $Id: arrays.inc 1433 2006-05-18 21:48:04Z austin $
 *
 * Purpose
 * Provide various functions for array handling.
 *
 */
use basic;
use math;
use os;
use uo;

/*
 * GetArrayRange(the_array, start, stop)
 *
 * Purpose
 * Retrieves a range from the array.
 *
 * Parameters
 * the_array:	The array to extract data from.
 * start:	Start index to get data from.
 * stop:	End index to get data from.
 *		If stop is 0, will go until the end of the array is reached.
 *
 * Return value
 * Returns an array on success.
 * Returns an error on failure.
 *
 */
function GetArrayRange(byref the_array, start, stop:=0)
	if ( Lower(TypeOf(the_array)) != "array" )
		return error{"errortext":="Object passed was not an array."};
	endif

    start := CInt(start);
    stop  := CInt(stop);

	if ( start < 1 )
		start := 1;
	endif
	if ( !stop )
		stop := the_array.Size();
	elseif ( stop > the_array.Size() )
		stop := the_array.Size();
	endif

	var new_array := array{};
	for ( start; start<=stop; start:=start+1 )
		new_array.Append(the_array[start]);
		sleepms(2);
	endfor

	return new_array;
endfunction

/*
 * BreakArray(the_array, size)
 *
 * Purpose
 * Breaks an array up into smaller arrays.
 *
 * Parameters
 * the_array:	The array that will be broken up.
 * size:	Size of the new arrays.
 *
 * Return value
 * Returns an array of arrays
 *
 */
function BreakArray(byref the_array, size:=1)
	if ( Lower(TypeOf(the_array)) != "array" )
		return error{"errortext":="Object passed was not an array."};
	endif

	if ( the_array.Size() > size )
		var new_arrays := array();
		var arrays_needed := Ceil(CDbl(the_array.Size()) / CDbl(size));

		var i := 1;
		var start := i;
		var stop := size;
		for ( i; i<=arrays_needed; i:=i+1 )
			new_arrays.Append(GetArrayRange(the_array, start, stop));
			start := stop+1;
			stop := stop+size;
			sleepms(2);
		endfor

		return new_arrays;
	else
		return array{the_array};
	endif
endfunction

/*
 * FindInArray(byref the_array, find_what)
 *
 * Purpose
 * Finds the first instance of something in an array.
 *
 * Parameters
 * the_array:	Array to search.
 * find_what:	What to look for.
 *
 * Return value
 * Returns > 1 (the index) if it is found.
 * Returns 0 if 'find_what' was not found.
 *
 */
function FindInArray(byref the_array, find_what)
	if ( Lower(TypeOf(the_array)) != "array" )
		return error{"errortext":="Object passed was not an array."};
	endif

	foreach index in ( the_array )
        if ((TypeOf(find_what) == "Struct" || TypeOf(index) == "Struct") || (TypeOf(index) == "Dictionary" || TypeOf(find_what) == "Dictionary"))
            var can_return := array{};
            foreach key in (find_what.Keys())
                if (index.Exists(key) && find_what[key] == index[key])
                    can_return.append(1);
                else
                    can_return.append(2);
                endif
                sleepms(2);
            endforeach

            if (!(2 in can_return))
                return _index_iter;
            endif
		elseif ( index == find_what )
			return _index_iter;
		endif
		sleepms(2);
	endforeach

	return 0;
endfunction

/*
 * FindElemInArray(byref the_array, find_what)
 *
 * Purpose
 * Finds the first instance of something in an array.
 *
 * Parameters
 * the_array:   Array to search.
 * find_what:   What to look for.
 *
 * Return value
 * Returns the elem of what its searching
 * Returns 0 if 'find_what' was not found.
 *
 */
function FindElemInArray(byref the_array, find_what)
    if ( Lower(TypeOf(the_array)) != "array" )
        return error{"errortext":="Object passed was not an array."};
    endif

    foreach index in ( the_array )
        if ((TypeOf(find_what) == "Struct" || TypeOf(index) == "Struct") || (TypeOf(index) == "Dictionary" || TypeOf(find_what) == "Dictionary"))
            var can_return := array{};
            foreach key in (find_what.Keys())
                if (index.Exists(key) && find_what[key] == index[key])
                    can_return.append(1);
                else
                    can_return.append(2);
                endif
                sleepms(2);
            endforeach

            if (!(2 in can_return))
                return index;
            endif
        elseif ( index == find_what )
			return index;
		endif
        sleepms(2);
    endforeach

    return 0;
endfunction

/*
 * FindAllElemInArray(byref the_array, find_what)
 *
 * Purpose
 * Finds the all instances of something in an array.
 *
 * Parameters
 * the_array:   Array to search.
 * find_what:   What to look for.
 *
 * Return value
 * Returns all elem of what its searching
 * Returns 0 if 'find_what' was not found.
 *
 */
function FindAllElemInArray(byref the_array, find_what)
    if ( Lower(TypeOf(the_array)) != "array" )
        return error{"errortext":="Object passed was not an array."};
    endif

    var to_return := array{};
    foreach index in ( the_array )
        if ((TypeOf(find_what) == "Struct" || TypeOf(index) == "Struct") || (TypeOf(index) == "Dictionary" || TypeOf(find_what) == "Dictionary"))
            foreach key in (find_what.Keys())
                if (index.Exists(key) && find_what[key] == index[key])
                    to_return.append(index);
                endif
                sleepms(2);
            endforeach
        elseif ( index == find_what )
			return to_return.append(index);
        endif
        sleepms(2);
    endforeach

	if (to_return.size() >= 1)
		return to_return;
	endif
	
    return 0;
endfunction


/*
 * RemoveFromArray(byref the_array, delete_what)
 *
 * Purpose
 * Remove any element mathing delete_what from the array.
 *
 * Parameters
 * the_array:	Array to search.
 * delete_what:	What to look for removing. Can pass an array to remove multiple elems.
 *
 * Return value
 * Returns the array without the elems
 *
 */
function RemoveFromArray(the_array, delete_what)
	if ( Lower(TypeOf(delete_what)) != "array")
		delete_what := array{delete_what};
	endif
	
	var iter_to_remove := array{};
	foreach item in (the_array)
		if (item in delete_what)
			iter_to_remove += _item_iter;
		endif
		sleepms(5);
	endforeach

	iter_to_remove.Reverse();
	foreach iter in iter_to_remove
		the_array.Erase(iter);
	endforeach

	return the_array;
endfunction

/*
 * RemoveLowest(byref the_array)
 *
 * Purpose
 * Remove lowest integer or real value from array
 *
 * Parameters
 * the_array:	Array to search.
 *
 * Return value
 * Returns the array without the lowest value
 *
 */
function RemoveLowest(the_array)
	if (the_array.Size() <= 1)
        return the_array;
	endif

	var i;
	var l_val := 1;
	for (i := 1; i < the_array.Size(); i+=1)
		if (the_array[l_val] > the_array[i])
			l_val := i;
		endif
	endfor

	the_array.Erase(l_val);
	return the_array;
endfunction

/*
 * RemoveHighest(byref the_array)
 *
 * Purpose
 * Remove lowest integer or real value from array
 *
 * Parameters
 * the_array:	Array to search.
 *
 * Return value
 * Returns the array without the highest value
 *
 */
function RemoveHighest(the_array)
	if (the_array.Size() <= 1)
        return the_array;
	endif

	var i;
	var l_val := 1;
	for (i := 1; i < the_array.Size(); i+=1)
		if (the_array[l_val] < the_array[i])
			l_val := i;
		endif
	endfor

	the_array.Erase(l_val);
	return the_array;
endfunction

/*
 * ArraySum(byref the_array)
 *
 * Purpose
 * Sum all integer/real values inside the array.
 *
 * Parameters
 * the_array:	Array to sum.
 *
 * Return value
 * Returns the value of sum of the values of the array
 *
 */
function ArraySum(the_array)
	if (the_array.Size() <= 1)
        return the_array[1];
	endif

	var f_val := 0;
	foreach entry in (the_array)
		f_val += entry;
	endforeach

	return f_val;
endfunction
Duttones, thank you very much! I think it will very helpful for many people!
Post Reply