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 23-Nov-2015 18:14:53

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

strbuild and strextr and how they work together

I was trying to figure out strbuild and strextr and how they work together thise weekend, or, rather, trying to remember what I forgot how to figure out. For whoever, including me, on how strbuild and strextr work to build lists of strings into strings and turn strings into lists of lists of strings, helps in handling text, especially if you want to do something meaningful with strings you build and turn them into list objects that you can step through to extrat information.

One thing out I found out (again, perhaps) is that there is no way to write the unmodified return value of strextr to the console/log, since there is no function to write a list of lists of strings to the console/log.

fun build() =
	strbuild ("harry"::"john"::"joe"::nil)::("sally"::"jenny"::"mary"::nil)::("maria"::"julia"::"fredonia"::nil)::nil;; 
	
fun main()=
	_showconsole; 
	
	let strextr build-> llist in
	let hd llist -> text in 
	let hd (tl llist) -> secondline in
	(
		
	_fooS strcat "the result of the command strbuild is (\"harry\"::\"john\"::\"joe\"::nil)::(\"sally\"::\"jenny\"::\"mary\"::nil)::(\"maria\"::\"julia\"::\"fredonia\"::nil)::nil; is a string, and this is the string: " build;
	_fooS "strextr takes a string of the format above, and whenever it encounters a string like (\"harry\"::\"john\"::\"joe\"::nil), it converts that substring into a list of words.  So strextr returns a list of lists, where each list is a list of words such as (\"harry\"::\"john\"::\"joe\"::nil). There is no scol function that displays a list of lists to the console so you have to break up the return value of strextr into parts, each of which is a list of strings, which you can print to the console using _fooSList. So the head of the list returned by strextr is itself a list, which can be written to the console using _fooSList, as below:\n ";
	_fooSList  text;
	_fooS "\n \n you can show the next line, which is the next list in the list of lists, as the head of the tail of the list of lists returned by strextr, as below: \n" ;
 	_fooSList secondline;
	_fooS " \n \n  to show the end of the list of lists returned by strext where you have three lines (which is three lists of strings followed by nil), which is the value \"nil\", you have to find the head of the tail of the tail of the tail of the list, which itself is a list of one item, which is always \"nil\"  \n  when you have a list containing a list of lists, it's easier to do a recursive function to shrink the tail of the list so you are not dealing with tails of tails of tails of tails of tails ad infinitu. Below is the head of the tail of the tail of the tail of list where the list has three lines, followed by the last list, which is just a stub, nil";
	_fooSList  (hd (tl( tl (tl llist))));
	);
	0;;
	

Last edited by hebdemnobad (23-Nov-2015 18:15:21)

Offline

#2 23-Nov-2015 19:18:58

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

Re: strbuild and strextr and how they work together

no but you can use a while on list of lines and then get the line [S r1] content for each lines
to check you can also write it directly in a file.

Offline

#3 23-Nov-2015 19:50:41

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

Re: strbuild and strextr and how they work together

arkeon wrote:

no but you can use a while on list of lines and then get the line [S r1] content for each lines
to check you can also write it directly in a file.

agreed, and fwiw, you can build a string with strcatn (something I touched on in another thread) and the \n  sequence and strextr will interpret the \n as the end of a list of words, so in

fun build2() =
	strcatn  "harry"::"john"::"joe"::"\n"::"sally"::"jenny"::"mary"::"\n"::"maria"::"julia"::"fredonia"::"\n"::nil;; 
	
	fun main()=
	_showconsole;
_fooSList  (hd (strextr build2));;
_fooSList  (hd (strextr build2));;

will print

harryjohnjoeNIL

to the console/log

so you can use either strbuild or strcatn to build a string that strextr can turn back into a list of lists of strings, with strcatn being eaiser in my opinion since you don't have to worry about sublists and () symbols

Last edited by hebdemnobad (23-Nov-2015 19:52:19)

Offline

#4 23-Nov-2015 21:50:04

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

Re: strbuild and strextr and how they work together

strbuild / strextr are commonly used to set arguments in the functions. By example, you can define a list of string parameters like for each name, a gender, a city, a phone, ...
They are also used to parse a long string.

To read or write it, you have several ways. Here are the most common :

fun fooFromName (list, name)=
	let switchstr list name -> lResult in
	let if !strcmp "m" nth_list lResult 0 then ["he" "him"] else ["she" "her"] -> [s t] in
	(
	_fooS sprintf "%s is %s years old, %s lives in %s. Set %s to call %s." [name hd tl lResult s hd tl tl lResult hd tl tl tl lResult t];
	// or, the line below displays the same thing :
	// _fooS sprintf "%s is %s years old, %s lives in %s. Set %s to call %s." [name nth_list lResult 1 s nth_list lResult 2 nth_list lResult 3 t];
	);;

// Return the same list
fun fooAllList (list)=
	if list == nil then
		nil
	else
		(_fooSList hd list) :: fooAllList tl list;;
		
fun fooLineOfList (list, number)=
	_fooSList nth_list list number;;

fun main ()=
	_showconsole;
	
	let ("John" :: "m" :: "25" :: "New York" :: "0123456789" :: nil) ::
	     ("Harry" :: "m" :: "44" :: "Boston" :: "2365478910" :: nil) ::
	     ("Sally" :: "f" :: "32" :: "Seattle" :: "541239870" :: nil ) ::
	     nil
	-> list in
		
	(
	_fooS "By name ...";
	fooFromName list "John";
	fooFromName list "Sally";
	
	_fooS "\nAll ...";
	fooAllList list;
	
	_fooS "\nItem 1 :";
	fooLineOfList list 1;
	
	0
	);;

In the log/console, you are :

By name ...
John is 25 years old, he lives in New York. Set 0123456789 to call him.
Sally is 32 years old, she lives in Seattle. Set 541239870 to call her.

All ...
John:m:25:New York:0123456789:NIL
Harry:m:44:Boston:2365478910:NIL
Sally:f:32:Seattle:541239870:NIL

Item 1 :
Harry:m:44:Boston:2365478910:NIL

With strcatn, this is not easy to parse the returned string.

Note : a text line by line can be get/set from lineextr / linebuild.
To extract a sublist, listextr may be a good choice. And more about list, see http://redmine.scolring.org/projects/sc … brary.html.

Offline

#5 23-Nov-2015 22:46:40

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

Re: strbuild and strextr and how they work together

With strcatn, this is not easy to parse the returned string.

No need to make things more difficult for myself and I'll follow that advice.

let ("John" :: "m" :: "25" :: "New York" :: "0123456789" :: nil) ::
	     ("Harry" :: "m" :: "44" :: "Boston" :: "2365478910" :: nil) ::
	     ("Sally" :: "f" :: "32" :: "Seattle" :: "541239870" :: nil ) ::
	     nil
	-> list in

writing lists this way does make them more readable I'll do that from now on, there's less chance of losing track of the () symbols and nil's.

Offline

#6 23-Nov-2015 23:11:48

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

Re: strbuild and strextr and how they work together

Directly print a list of strings lists is for debugging. A printer function should be written each time to format the output precisely.

Offline

#7 24-Nov-2015 11:16:30

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

Re: strbuild and strextr and how they work together

Offline

Board footer

Powered by FluxBB