Category talk:EasyLang: Difference between revisions

From Rosetta Code
Content added Content deleted
(Associative arrays)
(Add case conversion)
Line 1: Line 1:
Here are some common code snippets for implementing tasks in EasyLang.
Here are some common code snippets for implementing tasks in EasyLang.

==Find in array==
This function is for number arrays:
<syntaxhighlight lang="easylang">
func findInArray array[] item . index .
for i = 1 to len array[]
if array[i] = item
index = i
break 2
.
.
index = 0
.
</syntaxhighlight>
This function is for string arrays:
<syntaxhighlight lang="easylang">
func findInStrArray array$[] item$ . index .
for i = 1 to len array$[]
if array$[i] = item$
index = i
break 2
.
.
index = 0
.
</syntaxhighlight>


==Associative arrays==
==Associative arrays==
Line 53: Line 27:
.
.
item$ = ""
item$ = ""
.
</syntaxhighlight>

==Case conversion==
These functions only work with ASCII characters.
===Lowercase===
<syntaxhighlight lang="easylang">
func toLowercase string$ . result$ .
for i = 1 to len string$
code = strcode substr string$ i 1
if code >= 65 and code <= 90
code += 32
.
result$ &= strchar code
.
.
</syntaxhighlight>
===Uppercase===
<syntaxhighlight lang="easylang">
func toUppercase string$ . result$ .
for i = 1 to len string$
code = strcode substr string$ i 1
if code >= 97 and code <= 122
code -= 32
.
result$ &= strchar code
.
.
</syntaxhighlight>

==Find in array==
This function is for number arrays:
<syntaxhighlight lang="easylang">
func findInArray array[] item . index .
for i = 1 to len array[]
if array[i] = item
index = i
break 2
.
.
index = 0
.
</syntaxhighlight>
This function is for string arrays:
<syntaxhighlight lang="easylang">
func findInStrArray array$[] item$ . index .
for i = 1 to len array$[]
if array$[i] = item$
index = i
break 2
.
.
index = 0
.
.
</syntaxhighlight>
</syntaxhighlight>

Revision as of 10:13, 21 December 2022

Here are some common code snippets for implementing tasks in EasyLang.

Associative arrays

The syntax for an associative arrays in EasyLang should look like this:

associative$[][] = [ [ 1 "associative" ] [ 2 "arrays" ] ]

Indexing associative arrays

For associative number arrays:

func indexAssoc index . array[][] item .
   for i = 1 to len array[][]
      if array[i][1] = index
         item = array[i][2]
         break 2
      .
   .
   item = number "nan"
.

Foor associative string arrays:

func indexStrAssoc index$ . array$[][] item$ .
   for i = 1 to len array$[][]
      if array$[i][1] = index$
         item$ = array$[i][2]
         break 2
      .
   .
   item$ = ""
.

Case conversion

These functions only work with ASCII characters.

Lowercase

func toLowercase string$ . result$ .
   for i = 1 to len string$
      code = strcode substr string$ i 1
      if code >= 65 and code <= 90
         code += 32
      .
      result$ &= strchar code
   .
.

Uppercase

func toUppercase string$ . result$ .
   for i = 1 to len string$
      code = strcode substr string$ i 1
      if code >= 97 and code <= 122
         code -= 32
      .
      result$ &= strchar code
   .
.

Find in array

This function is for number arrays:

func findInArray array[] item . index .
   for i = 1 to len array[]
      if array[i] = item
         index = i
         break 2
      .
   .
   index = 0
.

This function is for string arrays:

func findInStrArray array$[] item$ . index .
   for i = 1 to len array$[]
      if array$[i] = item$
         index = i
         break 2
      .
   .
   index = 0
.