Page 1 of 2

List to Array

Posted: Thu Mar 05, 2009 8:56 pm
by melliott42
Hello,

If I have a comma delimited list in a character variable (cMyList). How can I use it to initialize a one dimensional array (aMyArray)?

Example Var:
cMyList := "A,B,C,D,E"

Id like to use the array to loop through the values.

Example goal - simple loop through an array:
local nLenArray := len(aMyArray)
for i := 1 TO nLenArray
MsgInfo( aMyArray )
next


Thanks,

Michael

Re: List to Array

Posted: Thu Mar 05, 2009 9:34 pm
by esgici
melliott42 wrote:Hello,

If I have a comma delimited list in a character variable (cMyList). How can I use it to initialize a one dimensional array (aMyArray)?

Michael
Hello Michael

Please take a look this sample:

Code: Select all

PROC Main()

   LOCAL cMyList := "A,B,C,D,E"
  
   LOCAL aMyArray := Strng2Arry( cMyList )
   
   ? cMyList 
   ?
   
   AEVAL( aMyArray, { | c1 | QOUT( c1 ) } )
   
   WAIT
   
   
RETU // Main()    

*.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._

FUNC Strng2Arry( cString )

   LOCA aRVal  := {},;
        c1Elem := ''
   
   TOKENINIT( cString, ',', 1 )
   
   WHILE !TOKENEND()
      c1Elem := TOKENNEXT( cString )
      AADD( aRVal, c1Elem )
   ENDDO
   
   TOKENEXIT()
   
RETU aRVal // Strng2Arry()

*.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._
Note that test portion of .prg for console mode. You have check "Mixed / Console mode" chekbox in the Tools\Preferences page.

I hope that helps you.

Regards

--

esgici

Re: List to Array

Posted: Fri Mar 06, 2009 12:07 am
by luisvasquezcl
Hola esgici,
excelente ejemplo,
todos los dias se aprende algo nuevo .... y cada dia me sorprendo más.
No tenia idea que existian las funciones tokenend y tokennext ... son de harbour o hmg?
saludos cordiales,

Hello esgici,
excellent example
every day you learn something new .... I am amazed every day and more.
I had no idea that there tokenend and functions tokennext ... are harbor or HMG?
best regards,

Luis Vàsquez

Re: List to Array

Posted: Fri Mar 06, 2009 12:36 am
by esgici
Hi All

Luis, thanks your question. Answer is : all *token* functions are from Harbour.

List is :

TOKENINIT : Initializes a token environment
TOKEN : Tokens of a string
TOKENSEP : Retrieves the token separators of the last token() call
ATTOKEN : Position of a token in a string
TOKENAT : Get start and end positions of tokens in a token environment
TOKENUPPER : Change the first letter of tokens to upper case
TOKENLOWER : Change the first letter of tokens to lower case
TOKENNEXT : Successivly obtains tokens from a string
NUMTOKEN : Retrieves the number of tokens in a string
TOKENEND : Check whether additional tokens are available with TOKENNEXT()
TOKENEXIT : Release global token environment
SAVETOKEN : Save the global token environment
RESTTOKEN : Restore global token environment
HB_ATOKENS : Split up a string, based on a delimiter.

Your question remind me HB_ATOKENS().

And, I want to correct the above sample:

Please extract Strng2Arry() function completely and change its calling line like this :

LOCAL aMyArray := HB_ATOKENS( cMyList, ',' )

System functions are always better than UDF :!:

Regards

--

esgici

Re: List to Array

Posted: Fri Mar 06, 2009 1:15 am
by Rathinagiri
It is just gr8. I didn't know about this either.

Still how many hidden treasures available?!

Re: List to Array

Posted: Fri Mar 06, 2009 1:51 am
by esgici
rathinagiri wrote:It is just gr8. I didn't know about this either.

Still how many hidden treasures available?!
Hello Rathinagiri

If you remember, I have said : this is a "Tantalos Torture". We have a real treasure and we can't know why we benefit from it, because of lack of documentation.

This isn't a exaggeration: Harbour became the biggest and most powerfull compiler of the world. Only deficiency is documentation.

Regards

--

esgici

Re: List to Array

Posted: Fri Mar 06, 2009 1:02 pm
by melliott42
esgici,

>> LOCAL aMyArray := HB_ATOKENS( cMyList, ',' )

That worked great! Thanks.

>> Only deficiency is documentation.

Does HMG have a Wiki? If not maybe we can create one so as the load to create and update the docs is done by the HMG user coummunity.

If this is something you are interested in I'd be glad to set one up for us. I recommend MediaWiki. That is what Wikipedia uses. It is easy to setup and easy to make changes to.

Michael

Re: List to Array

Posted: Fri Mar 06, 2009 4:06 pm
by luisvasquezcl
Hi,
It is true what they said esgici, documentation is an
the problems of harbor, that does not allow for andalusia
possible tool, which is very powerful. Moreover,
many other developers could see that it is not only a
clone clipper as presented but more than that.
Regards,
Luis VAsquez

Es cierto lo que comenta esgici, la documentación es uno
de los problemas de harbour, eso no permite aprovechar al
máximo la herramienta, la cual es muy poderosa. Además,
muchos otros programadores podrían ver que no es sólo un
clon de clipper como se presenta sino mucho más que eso.
Saludos
Luis VAsquez

Re: List to Array

Posted: Fri Mar 06, 2009 5:35 pm
by esgici
melliott42 wrote:
Does HMG have a Wiki? If not maybe we can create one so as the load to create and update the docs is done by the HMG user coummunity.

If this is something you are interested in I'd be glad to set one up for us. I recommend MediaWiki. That is what Wikipedia uses. It is easy to setup and easy to make changes to.
Hello Michael

Documentation deficiency is for Harbour, no HMG.

Harbour team works very hardly and don't have extra time for documentation. Anyway, good programmer are bad
documentors :) Still Roberto Lopez is an exception. HMG docs are perfect, samples also. Complete, short, concentrated, easy to understand and effective.

The "wiki" subject was discussed in Harbour Developpers Mail List. As far as I know, yet there isn't any neat result.

Thanks for your nice proposal. But I can't decide anything like this. Anyway, problem isn't setting up Wiki, is only to fill up it.

If you want, infos on Harbour mailing lists is here : http://www.harbour-project.org/faq/harbour34.html#34
( Warning : some infos may be obsolete :( )

Regards

--

esgici

Re: List to Array

Posted: Sat Mar 07, 2009 12:54 am
by melliott42
esgici,

Concerning the Wiki, no problem sir. :-)

>> HMG docs are perfect, samples also. Complete, short, concentrated, easy to understand and effective.

Yes I agree sir. Please let me offer great compliments on the teams efforts there. Great job!

Like you I want HMG not only to survive but be realized for just how good a development tool it is. Though you and I are long time Clipper guys I think presenting HMG in a way that makes it appear like a new (yet very mature) alternative to Java and .Net might be useful in many demographics.

The history of Clipper is like a tragic hero story. It was so good it evolved into a virtual religion. When it left Nantucket it started a downward spiral in the USA that never really recovered. HMG is the phoenix risen!

This sounds like I'm nuts but for anyone starting out in Clipper then having to write as much Visual Basic and Java as I have it was like getting used to being in a jail cell in comparison. The marketing efforts behind VB\Java overwhelmed common sense and practicality. Yep I am well off topic here...sorry. :-)

Michael