Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 29-Nov-2015 21:09:45

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

how to get the highest index from a list of struct objects

Edited to reflect Iri's observation below, although I'm still having problems with formatting indents from bluefish.

There is probably a list function that would make this easier, but, in any case, if you have a struct of whatever kind, and one of the fields of the struct, named  Iwhateverindex  is an interger, and you are trying to find the object that has the highest Iwhateverindex , this is how you can do it.  in this example, the Iwhateverindex  property is named Icylinderindex:

struct   Cylinder =[
	Icylindernumber:	I,
	Icylinderindex:	I,
	Fradius			: 	F,
	Fheight			: 	F,
	Tlifespan		:	Timer,	
	Sbirthfphrase	:	S,
	Sdeathphrase	:	S,
	fundyingclyinder:	fun [Cylinder] S,
	funreturnarea	:	fun [Cylinder] F,
	funreturnvolume:	fun[Cylinder] F
	
	]mkCylinder;;
	
typeof lCylinderlist=[Cylinder r1];;
typeof lDeleted_cylinder_ids= [I r1];;
typeof ICylinder_counter=		I;;
var Ihighest_Cylinder_Index= 0;;

fun get_highest_id_from_cylinderlist (cylinderlist, id)=
//we want to get the highest Cylinder.Icylinderindex that exists in this list!!!!!
if ((hd cylinderlist)==nil)&& ((sizelist lCylinderlist)==0) then
(
_fooS "there are no items in the list at all";
1;
)
else if ((hd cylinderlist)==nil)&& ((sizelist lCylinderlist)>0) then
(
_fooS "we are at the end of a list that has items in it";
_fooS strcat  "the following value is the highest value found in the complete list is: " (itoa id);
id;
) 
else
//we are not at the end of the list yet
(
_fooS "there are items in the list and we are not at the end yet";
let hd cylinderlist -> cylinder in
let cylinder.Icylinderindex -> this_index in
if (this_index > id) then
(
set id = this_index;
_fooS strcat "the highest id found so far is: " (itoa id);
//we havve the higghest id so far, recursively search through rest of list
//to see if we can find a higher id
get_highest_id_from_cylinderlist (tl cylinderlist) id;

)
else
(
//don't change the id, we have the highest found so far
//so we recursively search through rest of list
_fooS strcat "the highest id found so far is: " (itoa id);
get_highest_id_from_cylinderlist (tl cylinderlist) id;

);
);;


fun main()=
_showconsole;
let mkCylinder [1 879 nil nil nil nil nil nil nil nil]-> cylinder1 in
let mkCylinder [2 1056 nil nil nil nil nil nil nil nil]-> cylinder2 in
let mkCylinder [3 3456 nil nil nil nil nil nil nil nil]-> cylinder3 in
set lCylinderlist = cylinder1:: cylinder2::cylinder3::lCylinderlist; 
let nth_list lCylinderlist 0-> dcylinder in
let dcylinder.Icylinderindex -> index in
_fooS strcatn "the result of the index search through the list is:  ":: (itoa  (get_highest_id_from_cylinderlist lCylinderlist nil))::nil;;

Last edited by hebdemnobad (30-Nov-2015 14:49:00)

Offline

#2 29-Nov-2015 21:33:37

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,089
Website

Re: how to get the highest index from a list of struct objects

there is no magic way

parse all the list and get the max index found
let 0 -> maxid in
set maxid = max curid maxid;

of use sort functions in the lib to sort the list by id and get the first element.

All this possibilities take a lot of time on big lists

Offline

#3 29-Nov-2015 23:36:08

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: how to get the highest index from a list of struct objects

Your function is incorrect.

Try with this main function (i added a 4th cylinder with a shorter index) :

fun main()=
	_showconsole;
	let mkCylinder [1 879 nil nil nil nil nil nil nil nil]-> cylinder1 in
	let mkCylinder [2 1056 nil nil nil nil nil nil nil nil]-> cylinder2 in
	let mkCylinder [3 3456 nil nil nil nil nil nil nil nil]-> cylinder3 in
	let mkCylinder [4 2010 nil nil nil nil nil nil nil nil]-> cylinder4 in     // <- added !
	set lCylinderlist = cylinder1:: cylinder2::cylinder3::cylinder4::lCylinderlist; 
	let nth_list lCylinderlist 0-> dcylinder in
	let dcylinder.Icylinderindex -> index in
	_fooS strcatn "the result of the index search through the list is:  ":: (itoa  (get_highest_id_from_cylinderlist lCylinderlist nil))::nil;;

The output is :

there are items in the list and we are not at the end yet
the highest id found so far is: 879
there are items in the list and we are not at the end yet
the highest id found so far is: 1056
there are items in the list and we are not at the end yet
the highest id found so far is: 3456
there are items in the list and we are not at the end yet
the result of the index search through the list is:  0

0 ? Really ?

Try with this easier function :

fun get_highest_id_from_cylinderlist (cylinderlist, id)=
	if cylinderlist == nil then
	(
		_fooS "there are no items in the list at all";
		id
	)
	else
		let hd cylinderlist -> cylinder in
		if cylinder.Icylinderindex > id then
		(
			_fooS strcat "the highest id found so far is: " itoa cylinder.Icylinderindex;
			get_highest_id_from_cylinderlist tl cylinderlist cylinder.Icylinderindex
		)
		else
		(
			_fooS strcat "the current id is not greater than : " itoa id;
			get_highest_id_from_cylinderlist tl cylinderlist id
		);;

The output should be :

the highest id found so far is: 879
the highest id found so far is: 1056
the highest id found so far is: 3456
the current id is not greater than : 3456
there are no items in the list at all
the result of the index search through the list is:  3456

3456 seems correct.

As Arkeon suggests, you may use the max function.

You may also define a global variable to store the max value. When a new cylinder is created, you compare its index with this variable. If it is bigger, you replace it.

Something like that :

typeof maxCylinderId;;
...
fun cylinderNew (arg, id, ....)=
  let mkCylinder [... id ...] -> cylinder in
  (
  if id > maxCylinderId then
    set maxCylinderId = id;
  cylinder
  );;

to avoid to read the entire list each time.

PS : please, indent your code !!

Offline

#4 29-Nov-2015 23:42:45

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,089
Website

Re: how to get the highest index from a list of struct objects

in your text editor software change the tabulation setting to use 2 space character instead of tabulation

so the code is easier to copy / past.

Offline

#5 30-Nov-2015 00:51:20

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: how to get the highest index from a list of struct objects

Thanks for the correction Iri .....bluefish indents fone for me but I lose the tabs when I paste into posts. I will try changing that setting Arkeon.

[edit]  I checked out my code....I hadn't pasted all of it in and it looks more or like your correct function Iri.
But I get the point...if you can avoid stepping through a list, especially a big one, it's better to do that.

Last edited by hebdemnobad (30-Nov-2015 13:01:44)

Offline

Board footer

Powered by FluxBB