Category talk:EasyLang: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add case conversion)
(Replaced some procedures with equivalent functions)
 
(5 intermediate revisions by the same user not shown)
Line 7: Line 7:
For associative number arrays:
For associative number arrays:
<syntaxhighlight lang="easylang">
<syntaxhighlight lang="easylang">
func indexAssoc index . array[][] item .
proc indexAssoc index . array[][] item .
for i = 1 to len array[][]
for i = 1 to len array[][]
if array[i][1] = index
if array[i][1] = index
Line 17: Line 17:
.
.
</syntaxhighlight>
</syntaxhighlight>
Foor associative string arrays:
For associative string arrays:
<syntaxhighlight lang="easylang">
<syntaxhighlight lang="easylang">
func indexStrAssoc index$ . array$[][] item$ .
proc indexStrAssoc index$ . array$[][] item$ .
for i = 1 to len array$[][]
for i = 1 to len array$[][]
if array$[i][1] = index$
if array$[i][1] = index$
Line 34: Line 34:
===Lowercase===
===Lowercase===
<syntaxhighlight lang="easylang">
<syntaxhighlight lang="easylang">
func toLowercase string$ . result$ .
func$ toLowercase string$ .
for i = 1 to len string$
for i = 1 to len string$
code = strcode substr string$ i 1
code = strcode substr string$ i 1
Line 42: Line 42:
result$ &= strchar code
result$ &= strchar code
.
.
return result$
.
.
</syntaxhighlight>
</syntaxhighlight>
===Uppercase===
===Uppercase===
<syntaxhighlight lang="easylang">
<syntaxhighlight lang="easylang">
func toUppercase string$ . result$ .
func$ toUppercase string$ .
for i = 1 to len string$
for i = 1 to len string$
code = strcode substr string$ i 1
code = strcode substr string$ i 1
Line 54: Line 55:
result$ &= strchar code
result$ &= strchar code
.
.
return result$
.
.
</syntaxhighlight>
</syntaxhighlight>
Line 60: Line 62:
This function is for number arrays:
This function is for number arrays:
<syntaxhighlight lang="easylang">
<syntaxhighlight lang="easylang">
func findInArray array[] item . index .
func findInArray array[] item .
for i = 1 to len array[]
for i = 1 to len array[]
if array[i] = item
if array[i] = item
index = i
index = i
break 2
return index
.
.
.
.
index = 0
return index
.
.
</syntaxhighlight>
</syntaxhighlight>
This function is for string arrays:
This function is for string arrays:
<syntaxhighlight lang="easylang">
<syntaxhighlight lang="easylang">
func findInStrArray array$[] item$ . index .
func findInStrArray array$[] item$ .
for i = 1 to len array$[]
for i = 1 to len array$[]
if array$[i] = item$
if array$[i] = item$
index = i
index = i
break 2
return index
.
.
.
.
index = 0
return index
.
</syntaxhighlight>

==Sum and product of arrays==
<syntaxhighlight lang="easylang">
func sum array[] .
for item in array[]
result += item
.
return result
.
func product array[] .
result = 1
for item in array[]
result *= item
.
return result
.
.
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 04:57, 16 February 2024

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:

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

For associative string arrays:

proc 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$ .
   for i = 1 to len string$
      code = strcode substr string$ i 1
      if code >= 65 and code <= 90
         code += 32
      .
      result$ &= strchar code
   .
   return result$
.

Uppercase

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

Find in array

This function is for number arrays:

func findInArray array[] item .
   for i = 1 to len array[]
      if array[i] = item
         index = i
         return index
      .
   .
   return index
.

This function is for string arrays:

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

Sum and product of arrays

func sum array[] .
   for item in array[]
      result += item
   .
   return result
.
func product array[] .
   result = 1
   for item in array[]
      result *= item
   .
   return result
.