Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string parsing question

Status
Not open for further replies.

Nro

Programmer
May 15, 2001
337
CA
I’m sorry, but I think it’s a very simple question (and I’m a bit too lazy to search all around…)
I have a string delimited with / Ex: 5050/55050/5050/7707/5505/50606/… very very long. I want to create a table with one field and replace the field with the values, like that;

5050
55050
5050
7707
5505
50606

I have this very ugly routine to do this, and it’s very slow…

Code:
USE pipo.dbf
lcRet = "5050/55050/5050/7707/5505/50606/"
lcPro_Code = ""
DO WHILE .T.
 IF AT("/",lcRet,1) >= 1 THEN 
  lcPro_Code = LEFT(lcRet,AT("/",lcRet,1)-1)
  APPEND BLANK
  REPLACE Pro_Code WITH lcPro_Code 
  lcRet = SUBSTR(lcRet, AT("/",lcRet,1)+1)
 ELSE
  EXIT
 ENDIF
      
 IF LEN(lcRet) <= 1 THEN 
  EXIT
 ENDIF       
ENDDO

Thanks in advance

Nro
 
It would be easier to import from a file having N lines, wouldn't it?
So you would replace each / with carriage rerturn and line feed (the two chars needed for a new line in Windows).

There is STRTRAN for string "translation":

Code:
lcRet = "5050/55050/5050/7707/5505/50606/"
lcRet = STRTRAN(lcRet,"/",Chr(13)+Chr(10))

It "translates" each "/" into Chr(13)+Chr(10) (also known as CRLF)

Now after that you can put this into a file by STRTOFILE and then APPEND FROM that file.

Bye, Olaf.
 
Or another solution:

Use ALINES and the new to VFP8 or 9 parameter cParsechar and specify the "/"
Then you create an array of the numbers and can append it as records via APPEND FROM ARRAY. You will need to redium the array first, but it would not need a file written and read in again.

Bye, Olaf.
 
Thanks for the quick answer Olaf.
You are right, it’s better to convert / with LF CR like this. Lot faster

Code:
lcRet = STRTRAN(lcRet,"/",CHR(13) + CHR(10))
=STRTOFILE(lcRet,"Pipo.txt")

USE pipo.dbf EXCLUSIVE
ZAP
APPEND FROM "Pipo.txt" TYPE CSV

It’s working now.

Nro
 
So pipo.dbf only has the one field?

Then you can really also wortk with the array idea very simple:
Code:
USE pipo.dbf EXCLUSIVE
ZAP 
lcRet = "5050/55050/5050/7707/5505/50606/"
Dimension laRecords[ALines(laRecords,lcRet,4,"/"),1]
Append From Array laRecords

Dimension is creating an array with the length of the array ALines creates and a width of 1, which still is important to make the array 2-dimensional to be able to append it. It's a bit tricky as the inner ALINES is creating the array and the outer DIMENSION then is redimensioning it.

And last not least: Why not use a cursor at this point?
Create Cursor pipo (cPipo C(5)) && or whatever field type you need.
[ul][li]A cursor always is exclusive[/li]
[li]A cursor is hassle free, you don't need to think of a file location and name, just alias name[/li]
[li]A cursor vanishes with the datasession[/li]
[li]A cursor does not need a ZAP[/li]
[li]A cursor is in memory data, faster than DBF from hdd.[/li]
[/ul]

Bye, Olaf.
 
Allo Olaf

My problem with arrays is the amount of data. I can have more than 10,000 entries and I think there is some memory limit with VFP 9.

As for the Cursor, it’s a good idea. I’ll check this.

Thanks again for your help.

Nro
 
10,000 items in an array isn't a problem. In fact, it wasn't even a problem before VFP 9, because the limit was 65,000.

Tamar
 
And by the way: The 2GB limit also applies to files VFP creates with STRTOFILE. And there is string length limit of about 16MB, that would be your limit for initial strings with the "/" delimiter anyway, it's not that hard, it's just some string functions won't process more than 16MB string length. Anyway, you can't even get anywhere near 2GB.

And the element limit being gone is easily verified:
Code:
lcTest = Replicate("test"+Chr(13)+Chr(10),100000)
? ALines(laLines,lcTest)
? laLines[100000]

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top