Const Array String

General discussion on mikroBasic.
Post Reply
Author
Message
Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

Const Array String

#1 Post by Kalain » 09 Sep 2007 09:40

Hi,

Code like this is ok

Code: Select all

Const txt as string[8][2] =("AA","BB","CC","DD","EE","FF","GG","HH")
but not this one ! !

Code: Select all

const txt as string[8][1] = ("A","B","C","D","E","F","G","H")
It returns error message:
E-28 Too many characters (found 2 characters)

I don't understand why it's not possible to create a 2-dimensional array of 8x1 size.

These tries were to optimise code below with one line and a loop :

Code: Select all

  Glcd_Write_Text("A",63,3,1)
  Glcd_Write_Text("B",71,3,1)
  Glcd_Write_Text("C",79,3,1)
  Glcd_Write_Text("D",87,3,1)
  Glcd_Write_Text("E",95,3,1)
  Glcd_Write_Text("F",103,3,1)
  Glcd_Write_Text("G",111,3,1)
  Glcd_Write_Text("H",119,3,1)
Alain

Raslan
Posts: 758
Joined: 02 Jul 2006 12:31
Location: Syria & Finland
Contact:

#2 Post by Raslan » 09 Sep 2007 10:26

Hi,
That is a logic. Becuase the one dimentional array which you refer to Array [8][1]
is refered as simply Array[8] and there is no proint to write the one becuase 8 X 1 means 8 rows and 1 coloumn and we do not usually represents the one dimentional array in this way.

However, to optimize your code you may do the folloiwng.

Code: Select all

Const text as array[8] = ("A","B","C","D","E","F","G","H")
dim i as byte
dim j as word

j = 63                                         ' J variable is used to define the x location
for i = 0 to 8                                ' i variable is used to go through the array's elements
Glcd_Write_Text(array[i] , j ,3,1)
j = j + 8
next i
I am learning in this forum a lot from the others' problems and experinces...
"Give me a fish, I eat for a day. Teach me how to fish, I eat for a life time."

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#3 Post by Kalain » 09 Sep 2007 12:55

Raslan wrote:Hi,
That is a logic. Becuase the one dimentional array which you refer to Array [8][1]
is refered as simply Array[8] and there is no proint to write the one becuase 8 X 1 means 8 rows and 1 coloumn and we do not usually represents the one dimentional array in this way.
This is points like this one (for newbies) which should be mentionned in help mB file. This is not a problem when ou know but when you don't know , things which seems trivial are sometimes not easy......
However, to optimize your code you may do the folloiwng.

Code: Select all

Const text as array[8] = ("A","B","C","D","E","F","G","H")
dim i as byte
dim j as word

j = 63                                         ' J variable is used to define the x location
for i = 0 to 8                                ' i variable is used to go through the array's elements
Glcd_Write_Text(array[i] , j ,3,1)
j = j + 8
next i
Thanks.
As someone said previously somewhere in this forum :
Why things are so evident when it's done ?

Regards
Alain

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#4 Post by Kalain » 09 Sep 2007 21:50

Hi,

I modified a bit your code as below (array[x] replaced by string[x], and for i=0 to 8 replaced by for i = 0 to 7) but the code doesn't works. So Itried with this one to find a solution which works :

Code: Select all

Const text1 as string[8][2] = ("AA","BB","CC","DD","EE","FF","GG","HH")
dim i, j as byte

j = 63                ' J variable is used to define the x location
for i = 0 to 7       ' i variable is used to go through the array's elements
Glcd_Write_Text(text1[i] , j ,3,1)
j = j + 8
next i
This code works but I would need a single character for text1 so I wrote code below but doesn't compile and give this error (in Glcd_write_text instruction) :
Incompatible types (char to ?2)

Code: Select all

Const text1 as string[8] = ("A","B","C","D","E","F","G","H")
dim i, j as byte

j = 63                        ' J variable is used to define the x location
for i = 0 to 7              ' i variable is used to go through the array's elements
Glcd_Write_Text(text1[i] , j ,3,1)
j = j + 8
next i
It seems that multi array string works at least with 2 lengh string.
So I tried this one :

Code: Select all

Dim text1 as string[8]

dim i, j as byte

text1 = "ABCDEFGH"

j = 63                    ' J variable is used to define the x location
for i = 0 to 7          ' i variable is used to go through the array's elements
Glcd_Write_Text(text1[i], j ,3,1)
j = j + 8
next i
But still compilation error.

So I tried this solution which works :

Code: Select all

Dim text1 as string[8]
dim res as string[1] 

dim i, j as byte

text1 = "ABCDEFGH"

j = 63                    ' J variable is used to define the x location
for i = 0 to 7          ' i variable is used to go through the array's elements
res[0] = text1[i]
Glcd_Write_Text(res, j ,3,1)
j = j + 8
next i
Seems there is a problem with instruction like : Glcd_Write_Text(text1, j, 3, 1)
Alain

shish
Posts: 168
Joined: 05 Oct 2006 16:01
Location: Cheshire, UK

#5 Post by shish » 09 Sep 2007 22:26

the problem with your solution is it uses unnecessary ram

there must be something odd with constant multidimensional string arrays, any comment from the mE team?
[i]A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools. Douglas Adams.[/i]

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#6 Post by Kalain » 09 Sep 2007 22:33

Kalain wrote: So I tried this solution which works :

Code: Select all

Dim text1 as string[8]
dim res as string[1] 

dim i, j as byte

text1 = "ABCDEFGH"

j = 63                    ' J variable is used to define the x location
for i = 0 to 7          ' i variable is used to go through the array's elements
res[0] = text1[i]
Glcd_Write_Text(res, j ,3,1)
j = j + 8
next i
Seems there is a problem with instruction like : Glcd_Write_Text(text1, j, 3, 1)


Well, this solution seems to work but do not work properly because it display 'res' variable contents added by final null character!

I will have a look at solution suggested by MAN.
Alain

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#7 Post by xor » 10 Sep 2007 00:13

A string is assumed to be 2 or more characters in mB. A single character is either Char or Byte, and does not have a end of string 0. You can create a string of 2 characters and leave one space blank, so that the 2-dimension string has only a single character.

You cannot pass a constant string to the GLCD routines. Subroutines, such as the GLCD routines, will only accept RAM strings. This means that you must move the constant string into a RAM string before sending to the subroutine.

Code: Select all

const mytext as string[8] = "ABCDEFGH"
dim textstr as string[8]

textstr = mytext
GLCD_Write_Text(textstr,0,0,1)


Also, when dealing with single characters, declare them as Byte or Char. There is a GLCD routine for strings and another for single character/bytes. You can pass a constant Byte/Char to a subroutine:

Code: Select all

const mytext as byte[8] = ("A","B","C","D","E","F","G","H")

GLCD_Write_Char(mytext[1],0,0,1)
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#8 Post by Kalain » 10 Sep 2007 18:29

Hi xor,

Many thanks for your answer and your explanations. I works perfectly.
mB help file should really be updated with answers like yours.
There is already some informations in mB help file but not enough (from my point of view). When you know how to program something you do not need any (basic) informations but when you don't know how to do, any information (even basic) is very helpfull.

This will be profitable for :
- users because they would have some complete informations about how to use correctly mB.
- mE team, this would avoid them to answer to such basic problems.
- mB product. This avoid to suspect a bug. :roll:

Thanks again.
Alain

Raslan
Posts: 758
Joined: 02 Jul 2006 12:31
Location: Syria & Finland
Contact:

#9 Post by Raslan » 10 Sep 2007 20:34

Hi Kalain,
Kalain wrote: There is already some informations in mB help file but not enough (from my point of view).
Kalain wrote: This will be profitable for :
- users because they would have some complete informations about how to use correctly mB.
Then you must be intersted in this topic:

http://www.mikroe.com/forum/viewtopic.php?p=58434#58434

Best Regards
Raslan
I am learning in this forum a lot from the others' problems and experinces...
"Give me a fish, I eat for a day. Teach me how to fish, I eat for a life time."

p3t3rv
Posts: 268
Joined: 14 Apr 2005 09:33
Location: Doncaster UK

#10 Post by p3t3rv » 07 Oct 2007 19:02

As much as i love mickrobasic i have to agree with Kalain the help files are definatly lacking, maybe they should pay xor to re write the manual ;)
there are 10 types of people Those that understand binary and those that dont

trust issues
Posts: 231
Joined: 14 Nov 2004 19:43

#11 Post by trust issues » 07 Oct 2007 19:15

p3t3rv wrote:As much as i love mickrobasic i have to agree with Kalain the help files are definatly lacking, maybe they should pay xor to re write the manual ;)
Agreed!

Post Reply

Return to “mikroBasic General”