2

this is my issue

define varibale MyArray as character extent 40 no-undo.
define variable Mychara as character           no-undo.

Mychara = "hai this is checking how to copy values"

Now i want to copy this string to my "MyArray". So that it should be as follows

MyArray[1]=h ,MyArray[2]=a ,MyArray[3]=i ,MyArray[4]="" ,MyArray[5]=t ,MyArray[6]=h and so on...

So how to do it?

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
Bhavin Bhaskaran
  • 572
  • 5
  • 17
  • 41

3 Answers3

7

Given your code-example, this should do the trick:

define variable MyArr as character EXTENT 40 no-undo.
define variable Mychara as character           no-undo.

Mychara = "hai this is checking how to copy values".

DEF VAR i AS INT NO-UNDO.

DO i = 1 TO 40:
    MyArr[i] = SUBSTRING(MyChara,i,1).
END.

A caveat though: this means that you have to know the (maximum) size of your String beforehand, to define the array size appropriately.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
LyrixDeRaven
  • 538
  • 3
  • 9
2

little bit dynamically ;)

define variable MyArr   as character EXTENT no-undo.
define variable Mychara as character no-undo.
DEF    VAR      i       AS INT       NO-UNDO.

Mychara = "hai this is checking how to copy values".

EXTENT (MyArr) = LENGTH (Mychara).

DO i = 1 TO EXTENT (MyArr):
  MyArr[i] = SUBSTRING(MyChara,i,1).
END.
firhang
  • 244
  • 2
  • 11
1
define var l_mychara as integer no-undo.
define variable MyArray as character format "x(5)" extent 40 no-undo.
     define variable Mychara as character format "x(5)" no-undo.
def var i as int init 1.

Mychara = "hai this is checking how to copy values".
    assign l_mychara = length(Mychara).

do while i <= l_mychara.
    assign myarray[i] =   substring(mychara,i,1).
    if myarray[i] = "" then assign myarray[i] = "blank".
    i = i + 1.
   end.

disp Myarray .
ravi
  • 11
  • 1
  • Welcome to Stack Overflow. Please add some explanation to your code, otherwise, quite a neat answer' – qdot Oct 12 '12 at 00:15