Actions
String2Array¶
/* Copyright (C) 2011, Stephane Bisaro, aka iri <iri@irizone.net> License : you can do what you want with this source code This code is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */ /* Transform a string to an array setArray : fun [S] I initialize the array from a string. Return always 0. changeArray : fun [I S] I replace the value of the index. Return always 0. addArray : fun [S] I add a char at the end if there is a still space. Returns 0 if ok, 1 if there is not place enough, 2 if the length of input is too large (must be one char) removeArray : fun [I] I remove the content of the given index. The content of the superior index is decreased. Return 0 if ok, 1 if the index is out of range. getArrayValue : fun [I] S return the char from the given index getArray : fun [] S retrieve a string from the array Note : you can define the array with a max size instead the length of the input. In this case, don't forget to change tests in functions ... */ var input = "Scol is a bbeautiful language ";; typeof array = tab S;; /* Private functions */ fun getArray2 (counter)= if counter >= sizetab array then nil else strcat array.counter getArray2 counter+1;; fun addArray2 (array, char, previous, counter)= if counter < 0 then 1 else if array.counter == nil then addArray2 array char counter counter-1 else if previous != nil then ( set array.previous = char; 0 ) else addArray2 array char previous counter-1;; fun removeArray2 (array, index, counter)= if counter >= ((sizetab array) - 1) then ( set array.counter = nil; 0 ) else if counter >= index then ( set array.counter = array.(counter+1); removeArray2 array index counter+1 ) else removeArray2 array index counter+1;; fun setArray2 (string, counter)= if (!strcmp string "") then 0 else ( set array.counter = substr string 0 1; setArray2 substr string 1 strlen string counter+1 );; /* Public functions */ fun getArray ()= getArray2 0;; fun getArrayValue (index)= array.index;; fun removeArray (index)= if index > sizetab array then 1 else removeArray2 array index 0;; fun addArray (char)= if 1 == strlen char then let (sizetab array)-1 -> n in addArray2 array char nil n else 2;; fun changeArray (index, char)= set array.index = char; 0;; fun setArray (string)= set array = mktab strlen string ""; setArray2 string 0;; /* Main function */ fun main ()= _showconsole; _fooS input; _fooId setArray input; _fooS getArrayValue 12; _fooId changeArray 2 "O"; _fooId removeArray 10; _fooId addArray "!"; _fooS getArray; 0;;
Author : iri
Date : december 2011
Return to Examples
Updated by iri almost 13 years ago ยท 2 revisions