For Programmers: Free Programming Magazines  


Home > Archive > Clipper > February 2006 > Arrays









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Arrays
Robert J. Stuart

2006-01-10, 3:55 am

OK sorry it this seems dumb but I don't understand the syntax for
muldimentional arrays.
I understand to create one:

aArray := Array(10,5)

fine this gives me 10 elements with 5 elements in each one got that part
Now how do I assign value or extract value

I tried x=aArray(1,1)

that didn't work

nor did Afill(aArray,.t.,.f.)

I can extract the base element of aArray(x) but not the second elements.

What am I missing?

Bob S.


Bruce Boddington

2006-01-10, 3:55 am

Robert

You access array elements using square brackets:
e.g.
aArray[4,6]

HTH

Cheers

Bruce Boddington


"Robert J. Stuart" <rjstuart@earthlink.net> wrote in message
news:ioJrf.9664$3Z.8617@newsread1.news.atl.earthlink.net...
> OK sorry it this seems dumb but I don't understand the syntax for
> muldimentional arrays.
> I understand to create one:
>
> aArray := Array(10,5)
>
> fine this gives me 10 elements with 5 elements in each one got that part
> Now how do I assign value or extract value
>
> I tried x=aArray(1,1)
>
> that didn't work
>
> nor did Afill(aArray,.t.,.f.)
>
> I can extract the base element of aArray(x) but not the second elements.
>
> What am I missing?
>
> Bob S.
>
>



Joe Wright

2006-01-10, 3:55 am

Robert J. Stuart wrote:
> OK sorry it this seems dumb but I don't understand the syntax for
> muldimentional arrays.
> I understand to create one:
>
> aArray := Array(10,5)
>
> fine this gives me 10 elements with 5 elements in each one got that part
> Now how do I assign value or extract value
>
> I tried x=aArray(1,1)
>
> that didn't work
>

aArray[1][1] := x

> nor did Afill(aArray,.t.,.f.)
>
> I can extract the base element of aArray(x) but not the second elements.
>
> What am I missing?
>

Practice.

> Bob S.
>
>



--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Robert J. Stuart

2006-01-10, 3:55 am

I have tried

aArray := array(10,5)
afill(aArray,.t.,.f.)

atest := aArray[x,y]

or

atest := aArray[x][y]

and in both cases I get an array access error

if I do

atest := aArray[x]

atest will result in .t.

so I can access the first element ok just like a single dimension array in
S87
I am having problems understanding the syntax for the second element




Bob S.


"Robert J. Stuart" <rjstuart@earthlink.net> wrote in message
news:ioJrf.9664$3Z.8617@newsread1.news.atl.earthlink.net...
> OK sorry it this seems dumb but I don't understand the syntax for
> muldimentional arrays.
> I understand to create one:
>
> aArray := Array(10,5)
>
> fine this gives me 10 elements with 5 elements in each one got that part
> Now how do I assign value or extract value
>
> I tried x=aArray(1,1)
>
> that didn't work
>
> nor did Afill(aArray,.t.,.f.)
>
> I can extract the base element of aArray(x) but not the second elements.
>
> What am I missing?
>
> Bob S.
>
>



s_boomer

2006-01-10, 3:55 am


Robert J. Stuart wrote:[color=darkred]
> I have tried
>
> aArray := array(10,5)
> afill(aArray,.t.,.f.)
>
> atest := aArray[x,y]
>
> or
>
> atest := aArray[x][y]
>
> and in both cases I get an array access error
>
> if I do
>
> atest := aArray[x]
>
> atest will result in .t.
>
> so I can access the first element ok just like a single dimension array in
> S87
> I am having problems understanding the syntax for the second element
>
>
>
>
> Bob S.
>
>
> "Robert J. Stuart" <rjstuart@earthlink.net> wrote in message
> news:ioJrf.9664$3Z.8617@newsread1.news.atl.earthlink.net...

sali

2006-01-10, 3:55 am

"Robert J. Stuart" <rjstuart@earthlink.net> wrote in message
news:uJWrf.9829$3Z.5460@newsread1.news.atl.earthlink.net...
> I have tried
>
> aArray := array(10,5)
> afill(aArray,.t.,.f.)
>
> atest := aArray[x,y]
>
> or
>
> atest := aArray[x][y]
>
> and in both cases I get an array access error
>
> if I do
>
> atest := aArray[x]
>
> atest will result in .t.
>
> so I can access the first element ok just like a single dimension array in
> S87
> I am having problems understanding the syntax for the second element
>
>
>
>
> Bob S.
>
>
> "Robert J. Stuart" <rjstuart@earthlink.net> wrote in message
> news:ioJrf.9664$3Z.8617@newsread1.news.atl.earthlink.net...
>



basicly, clipper arrays are single dim arrays, but each element may be of
any type, so it may be also another array.

a:=array(5) makes one dim array of 5 elem, each of them notinitialized,
empty (nil)
a[1]:=array(3) makes first elem of that array to be another array, so you
may have a[1][3]:=something

when you say a:=array(10,5) you make base array of length 10, end each
element to be another array length 5
this is similar to [just short way to define]
a:=array(10)
for i:=1 to 10
a[i]:=array(5)
next

to fill theses arrays, you need
for i:=1 to len(a)
afill(a[i],"something")
next

or the same, but with explicit, elementary assignment ("afill()" is a
special function)
for i:=1 to len(a)
for j:=1 to len(a[i])
a[i][j]:="something"
next
next

when you say afill(a,.f.) you say that mentioned base array [which each
elemet contains another array] to be filled with .f., thus destroying
initial two-dim array, and make a single dim array.

clipper's arrays are one of the most powerfull things i've seen across many
languages [of course, those arrays are missing few features, but you may
compensate for them]



Robert J. Stuart

2006-01-10, 3:55 am

OK I got it now. When I was doing an afill(), I was screwing up the array
and making it a single dimension again.
What I was trying to do is assign if anyone is entered in an event. I have
10 different events with 5 positions per event. So X was the event number
while Y was the 5 different positions. aArray := array(10,5). But I was
then trying to set the default of .f. to all of the elements with
afill(aArray,.f.). At that point I must be making it a single array then.
So I did

for x := 1 to 10
afill(aArray[x],.f.)
next

and that did it.

got it now thanks for the explanation.

Bob S.


"sali" <sali@tel.net.ba> wrote in message
news:dopq1d$3dr$1@ss405.t-com.hr...
> "Robert J. Stuart" <rjstuart@earthlink.net> wrote in message
> news:uJWrf.9829$3Z.5460@newsread1.news.atl.earthlink.net...
in[color=darkred]
part[color=darkred]
elements.[color=darkred]
>
>
> basicly, clipper arrays are single dim arrays, but each element may be of
> any type, so it may be also another array.
>
> a:=array(5) makes one dim array of 5 elem, each of them notinitialized,
> empty (nil)
> a[1]:=array(3) makes first elem of that array to be another array, so you
> may have a[1][3]:=something
>
> when you say a:=array(10,5) you make base array of length 10, end each
> element to be another array length 5
> this is similar to [just short way to define]
> a:=array(10)
> for i:=1 to 10
> a[i]:=array(5)
> next
>
> to fill theses arrays, you need
> for i:=1 to len(a)
> afill(a[i],"something")
> next
>
> or the same, but with explicit, elementary assignment ("afill()" is a
> special function)
> for i:=1 to len(a)
> for j:=1 to len(a[i])
> a[i][j]:="something"
> next
> next
>
> when you say afill(a,.f.) you say that mentioned base array [which each
> elemet contains another array] to be filled with .f., thus destroying
> initial two-dim array, and make a single dim array.
>
> clipper's arrays are one of the most powerfull things i've seen across

many
> languages [of course, those arrays are missing few features, but you may
> compensate for them]
>
>
>



Robert Haley

2006-01-10, 3:55 am

Bob,
[color=darkred]

The syntax to fill a multidimensional array vs. S'87 is different: As
written, you are (resetting) your multidimensional array to a
single-dimension (10) element array by replacing each subarray element
with logical True (.T.)

To use AFILL() with a multidimensional array, you must instruct AFILL()
to operate on each individual subarray,

To assign logical True .T. to all elements of each subarray, use:

FOR x := 1 to 10 // or LEN( aArray )
AFILL( aArray[x], .T. )
NEXT // x

Internally your array should now look like:

{ ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } , ;
{ .T., .T., .T., .T., .T. } ;
}

HTH & Regards,
Bob

Robert J. Stuart

2006-01-10, 3:55 am

Yep Thanks Bob I got it now. Just took a little while for that light bulb
to turn on for me.


Bob S.

"Robert Haley" <r_haley_3rd@email.com> wrote in message
news:1135663225.057705.171310@g49g2000cwa.googlegroups.com...
> Bob,
>
>
> The syntax to fill a multidimensional array vs. S'87 is different: As
> written, you are (resetting) your multidimensional array to a
> single-dimension (10) element array by replacing each subarray element
> with logical True (.T.)
>
> To use AFILL() with a multidimensional array, you must instruct AFILL()
> to operate on each individual subarray,
>
> To assign logical True .T. to all elements of each subarray, use:
>
> FOR x := 1 to 10 // or LEN( aArray )
> AFILL( aArray[x], .T. )
> NEXT // x
>
> Internally your array should now look like:
>
> { ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } , ;
> { .T., .T., .T., .T., .T. } ;
> }
>
> HTH & Regards,
> Bob
>



Chris McGraw

2006-02-02, 3:55 am

Robert J. Stuart wrote:
aDbf := {}
aadd(adbf, {'KEY' , 'C', 31, 0})
aadd(adbf, {'STOREID' , 'C', 5, 0})
aadd(adbf, {'DATE' , 'C', 13, 0})
aadd(adbf, {'TIME' , 'C', 5, 0})
aadd(adbf, {'PRODID' , 'C', 12, 0})
aadd(adbf, {'DESC' , 'C', 30, 0})
aadd(adbf, {'QUANTITY' , 'C', 5, 0})
aadd(adbf, {'PRICE' , 'C', 9, 0})
aadd(adbf, {'REMOVE' , 'C', 1, 0})

Access goes like this
adbf[1][1] gives you the word "KEY" TXT
adbf[1][2] gives you "C" TXT
adbf[1][3] gives you 31 VAL
adbf[1][4] gives you 0 VAL

so

adbf[6][3] will give you back 30 VAL

I hope this helps
Cheers
Chris McGraw



> OK sorry it this seems dumb but I don't understand the syntax for
> muldimentional arrays.
> I understand to create one:
>
> aArray := Array(10,5)
>
> fine this gives me 10 elements with 5 elements in each one got that part
> Now how do I assign value or extract value
>
> I tried x=aArray(1,1)
>
> that didn't work
>
> nor did Afill(aArray,.t.,.f.)
>
> I can extract the base element of aArray(x) but not the second elements.
>
> What am I missing?
>
> Bob S.
>
>

Dave P

2006-02-02, 6:55 pm

to help out with arrays
local a1
local a2
a1:=array(10)
//a1 will look like this
a1[1]
a1[2]
a1[3]
each element of A1 has 1 Position
a2:=array(10,2)
a2[1][1] ,a2[1][2]
a2[2][1],a2[2][2]
two dim array will look like the above
each element has 2 positions Two dimensional arrays
Resemble a Database record many down many accross
for x:=1 to len(a2)
for y:=1 to len(a2[x])
? a2[x][y]
next
next
//3 dimensional array are Ugly i use them for index creations
a3:=array(3)
a3[1]:={"",NIL}
A3[1][2]:={{"1ordername","1orderkey",ordfor,ordwhile,decending,unique,"bagna
me","description},;
{"2ordname","key",for,while,decend,unique,bag,desc}}
this array looks like the below
a3[1][1]==database name
a3[1][2]==array of Index/order information
a3[1][y][1] ///1st order
a3[1][y][2] // 2nd order

in a loop
for x:=1 to len(a3)
for y:=1 to len(a3[x][2])
for x:=1 to len(a3[x][y][z])
? a3[x][1] //database name
?? a3[x][2][y][z] // each individual order field definition
for specific database
next
next
next
somthing like the above
Hope all the above helps array explanation
Dave


"Chris McGraw" <cyber~nexus@heydon.com> wrote in message
news:nxgEf.8721$Iw6.611946@news20.bellglobal.com...[color=darkred]
> Robert J. Stuart wrote:
> aDbf := {}
> aadd(adbf, {'KEY' , 'C', 31, 0})
> aadd(adbf, {'STOREID' , 'C', 5, 0})
> aadd(adbf, {'DATE' , 'C', 13, 0})
> aadd(adbf, {'TIME' , 'C', 5, 0})
> aadd(adbf, {'PRODID' , 'C', 12, 0})
> aadd(adbf, {'DESC' , 'C', 30, 0})
> aadd(adbf, {'QUANTITY' , 'C', 5, 0})
> aadd(adbf, {'PRICE' , 'C', 9, 0})
> aadd(adbf, {'REMOVE' , 'C', 1, 0})
>
> Access goes like this
> adbf[1][1] gives you the word "KEY" TXT
> adbf[1][2] gives you "C" TXT
> adbf[1][3] gives you 31 VAL
> adbf[1][4] gives you 0 VAL
>
> so
>
> adbf[6][3] will give you back 30 VAL
>
> I hope this helps
> Cheers
> Chris McGraw
>
>
>


sali

2006-02-08, 8:52 am

"Dave P" <dvs_bis@sbcglobal.net> wrote in message
news:YQpEf.40923$dW3.40163@newssvr21.news.prodigy.com...[color=darkred]
> to help out with arrays
>
> "Chris McGraw" <cyber~nexus@heydon.com> wrote in message
> news:nxgEf.8721$Iw6.611946@news20.bellglobal.com...

another funny thing:

row=adbf[1]

now "row" points to the same memory location as "adbf[1]" does, and you may
do:

?adbf[1][2] // result-> "C"
row[2]:="N"
?adbf[1][2] // result-> "N", value changed transparently

so when having complex multidim array, you may shorten and speed up code,
assigning auxilary var array to subdimension




sali

2006-02-08, 8:52 am

[test]


Dave P

2006-02-08, 8:52 am

depending on what you are doing assignng
Subarrays too single dim arrays will slow things down
depending on the size
and 4000 elements is not that many
local amain, asub
aMain:=Array(3000)
asub:=100
for x:=1 too 3000
amain[x]:=aclone(aSub) // new reference
next
takes like 1 second
or

amain:=array(4000,100)

the array looks like this
just lik a database
row1 field1,field2,field3
row2
row3
etc
how you build your arrays can be slow or fast
i use the above method as i place a sub array into the main array
i set the column values
or change aclone with a scatter/gather
//i know i can have upto 50 transactions a year
local amain,x
amain:=array(50)
x:=0
do while trans->transno==cTransno .and. ! trans->(eof())
x++
if x>=50
aadd(amain,trans->(scatter())
else
amain[x]:=trans->(scatter()) /// one record in the array
endif
trans->(dbskip())
enddo
if x<50
asize(amain,x) // there was not 50 transactions
endif

look at the code above...there is no aadd() unless count is above 50
aadd is a great function..but its really a slow poke in loops like above
so if you have many array elements too create..create the array before hand
and re-size it at the end

dave


sali

2006-02-08, 8:52 am

"Dave P" <dvs_bis@sbcglobal.net> wrote in message
news:IwKFf.52273$PL5.27955@newssvr11.news.prodigy.com...
> depending on what you are doing assignng
> Subarrays too single dim arrays will slow things down


i was talking about speeding up access to elements of array's last
dimension, when there are many dims.
like arraymain[n1][n2][n3][n4][i]
having arraysub=arraymain[n1][n2][n3][n4], it is the same effect [both
read/write]

arraysub[i] and
arraymain[n1][n2][n3][n4][i]

but clipper don't need to recalc each dim [n1 ..n4] every time when
accessing last [i] memory loc.

this thing allow me to keep very complex data at the single array, but still
to have fast access of last dim


Dave P

2006-02-08, 8:52 am

once your array is created...you have access too all elements...
its the allocation part depending the size of the array that takes time

dave

"sali" <sali@euroherc.hr> wrote in message
news:ds9hrr$k0q$1@ss405.t-com.hr...
> "Dave P" <dvs_bis@sbcglobal.net> wrote in message
> news:IwKFf.52273$PL5.27955@newssvr11.news.prodigy.com...
>
> i was talking about speeding up access to elements of array's last
> dimension, when there are many dims.
> like arraymain[n1][n2][n3][n4][i]
> having arraysub=arraymain[n1][n2][n3][n4], it is the same effect [both
> read/write]
>
> arraysub[i] and
> arraymain[n1][n2][n3][n4][i]
>
> but clipper don't need to recalc each dim [n1 ..n4] every time when
> accessing last [i] memory loc.
>
> this thing allow me to keep very complex data at the single array, but

still
> to have fast access of last dim
>
>



sali

2006-02-08, 8:52 am

"Dave P" <dvs_bis@sbcglobal.net> wrote in message
news:WL2Gf.18586$_S7.1826@newssvr14.news.prodigy.com...
> once your array is created...you have access too all elements...
> its the allocation part depending the size of the array that takes time
>
> dave
>



if you don't mind, make this test, please

local amain,asub,i,value,ii

amain={{{{{{{{{{{{{1,2,3,4,5,6,7,8,9,10}
}}}}}}}}}}}}
asub=amain[1][1][1][1][1][1][1][1][1][1]
[1][1]

*test loop1
for ii=1 to milion
for i=1 to 10
value=asub[i]
next
next

*test loop2
for ii=1 to milion
for i=1 to 10
value=amain[1][1][1][1][1][1][1][1][1][1
][1][1][i]
next
next

put enough large number for "milion", and measure run time of two loops of
array access time
accessing a single dim array asub, pointing to the last dim of large array
amain should gain much time preserve

of course, amain array is shown very "thin", realy, it may have any number
of elements in each dim


> "sali" <sali@euroherc.hr> wrote in message
> news:ds9hrr$k0q$1@ss405.t-com.hr...
> still
>
>



Dave P

2006-02-08, 8:52 am

may i Ask What are you trying too accomplish

dave

"sali" <sali@euroherc.hr> wrote in message
news:dsafd2$6v2$1@ss405.t-com.hr...
> "Dave P" <dvs_bis@sbcglobal.net> wrote in message
> news:WL2Gf.18586$_S7.1826@newssvr14.news.prodigy.com...
>
>
> if you don't mind, make this test, please
>
> local amain,asub,i,value,ii
>
> amain={{{{{{{{{{{{{1,2,3,4,5,6,7,8,9,10}
}}}}}}}}}}}}
> asub=amain[1][1][1][1][1][1][1][1][1][1]
[1][1]
>
> *test loop1
> for ii=1 to milion
> for i=1 to 10
> value=asub[i]
> next
> next
>
> *test loop2
> for ii=1 to milion
> for i=1 to 10
> value=amain[1][1][1][1][1][1][1][1][1][1
][1][1][i]
> next
> next
>
> put enough large number for "milion", and measure run time of two loops of
> array access time
> accessing a single dim array asub, pointing to the last dim of large array
> amain should gain much time preserve
>
> of course, amain array is shown very "thin", realy, it may have any number
> of elements in each dim
>
>
>
>



Dave P

2006-02-08, 8:52 am

looks like ur going through 10,000,000
for x:=1 too 1000000
for y:=1 too 10
next
next
all your doing is doing long loops and reading a position
of a very small (array definition)
you created 1 dim array with 12 empty(sub array)
at the 13th position u have put in some elements
if you start diminsioning the array you will
hit the string overflow error most likely

all i see is you going through placing the value of the 12th
y=1 too 10 into a reg variable

the total count is like 10,000,000 million iterations
and there is nothing being accomplished

let me know what you are trying too accomplish

dave p

dave

"sali" <sali@euroherc.hr> wrote in message
news:dsafd2$6v2$1@ss405.t-com.hr...
> "Dave P" <dvs_bis@sbcglobal.net> wrote in message
> news:WL2Gf.18586$_S7.1826@newssvr14.news.prodigy.com...
>
>
> if you don't mind, make this test, please
>
> local amain,asub,i,value,ii
>
> amain={{{{{{{{{{{{{1,2,3,4,5,6,7,8,9,10}
}}}}}}}}}}}}
> asub=amain[1][1][1][1][1][1][1][1][1][1]
[1][1]
>
> *test loop1
> for ii=1 to milion
> for i=1 to 10
> value=asub[i]
> next
> next
>
> *test loop2
> for ii=1 to milion
> for i=1 to 10
> value=amain[1][1][1][1][1][1][1][1][1][1
][1][1][i]
> next
> next
>
> put enough large number for "milion", and measure run time of two loops of
> array access time
> accessing a single dim array asub, pointing to the last dim of large array
> amain should gain much time preserve
>
> of course, amain array is shown very "thin", realy, it may have any number
> of elements in each dim
>
>
>
>



Dave Pearson

2006-02-08, 6:55 pm

* Dave P <dvs_bis@sbcglobal.net>:

> let me know what you are trying too accomplish


It looks to me like he's trying to illustrate, with code you can play with,
that:

,----
| a[ 1 ][ 1 ][ 1 ][ 1 ][ 1 ][ 1 ][ 1 ][ 1 ]
`----

has more expressions in it than:

,----
| a[ 1 ]
`----

--
Dave Pearson | OSLib - Timeslice release functions.
http://www.davep.org/ | eg - Norton Guide reader for Linux.
http://www.davep.org/clipper/ | weg - Norton Guide reader for Windows.
http://www.davep.org/norton-guides/ | dgscan - DGROUP scanner for Clipper.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com