List to Array

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

List to Array

Post by melliott42 » Thu Mar 05, 2009 8:56 pm

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

User avatar
esgici
Posts: 4446
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 332 times
Been thanked: 100 times
Contact:

Post by esgici » Thu Mar 05, 2009 9:34 pm

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
Viva INTERNATIONAL HMG :D

User avatar
luisvasquezcl
Posts: 1086
Joined: Thu Jul 31, 2008 3:23 am
Location: Chile
Has thanked: 13 times
Been thanked: 14 times
Contact:

Post by luisvasquezcl » Fri Mar 06, 2009 12:07 am

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

User avatar
esgici
Posts: 4446
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 332 times
Been thanked: 100 times
Contact:

Post by esgici » Fri Mar 06, 2009 12:36 am

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
Viva INTERNATIONAL HMG :D

User avatar
Rathinagiri
Posts: 5222
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 158 times
Been thanked: 153 times
Contact:

Post by Rathinagiri » Fri Mar 06, 2009 1:15 am

It is just gr8. I didn't know about this either.

Still how many hidden treasures available?!
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
esgici
Posts: 4446
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 332 times
Been thanked: 100 times
Contact:

Post by esgici » Fri Mar 06, 2009 1:51 am

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
Viva INTERNATIONAL HMG :D

melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Post by melliott42 » Fri Mar 06, 2009 1:02 pm

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

User avatar
luisvasquezcl
Posts: 1086
Joined: Thu Jul 31, 2008 3:23 am
Location: Chile
Has thanked: 13 times
Been thanked: 14 times
Contact:

Post by luisvasquezcl » Fri Mar 06, 2009 4:06 pm

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

User avatar
esgici
Posts: 4446
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 332 times
Been thanked: 100 times
Contact:

Post by esgici » Fri Mar 06, 2009 5:35 pm

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
Viva INTERNATIONAL HMG :D

melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Post by melliott42 » Sat Mar 07, 2009 12:54 am

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

Post Reply