×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Manipulating long text lines, similar to fields in a table
3

Manipulating long text lines, similar to fields in a table

Manipulating long text lines, similar to fields in a table

(OP)
Hi,

Normally if I have a text file with line lengths less than 255, I can APPEND the text file into a table, manipulate the lines in the table, then output the changes to another text file.

But the text files I'm working with, have line lengths of around 1000 characters. The line length is uniform throughout the text file.

Is there a way to manipulate the text file, like e.g. REPLACing the minuses in the BBB line types with pluses, and replacing the pluses with minuses (essentially reversing all the amounts in the text file)?

I'm trying to do it from the REPL/DOT PROMPT, rather than writing a script.

If it's necessary, I'll write the script, and probably populate an array with the text file, then loop through it and do the replace. I just wanted to see if it's possible to do this manipulation manually.


AAA FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE
BBB -0000123.00ABCD
BBB +0000777.00ABCD
BBB +0000845.00ABCD
CCC HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
DDD 39292983987AABBBBCCCCC

RE: Manipulating long text lines, similar to fields in a table

Hi,

You may want to have a look at the MEMO field type, the EDITBOX control and the functions ALINES(), ATLINE(), MLINE() ...

hth

MarK

RE: Manipulating long text lines, similar to fields in a table

You can read a file into a variable with FILETOSTR(), then manipulate that variable with STRTRAN() and then save it with STRTOFILE().
Since you want to replace quite near to the line start, this could work like this:

CODE

lcFileName = GETFILE()
lcFile = FILETOSTR(lcFileName)
STRTRAN(lcFile,CHR(13)+CHR(10)+'BBB +',CHR(13)+CHR(10)+'BBB -')
STRTRAN(lcFile,CHR(13)+CHR(10)+'BBB -',CHR(13)+CHR(10)+'BBB +')
STRTOFILE(lcFile,lcFilename) 

I took 'BBB' literally in this case. If it varies what's before the first sign of a number or there are further numbers later in the lines, it's recommendable to do something in the sense of fully understanding the file format and read it into a table with the proper structure of multiple fields.

Chriss

RE: Manipulating long text lines, similar to fields in a table

You could also prepare a DBF with multiple char fields, for example five C(254) fields, to use in such occasions and then APPEND FROM (GETFILE()) TYPE SDF.


Chriss

RE: Manipulating long text lines, similar to fields in a table

If each line consists of a number of logical fields, and if those fields are of uniform length throught the file, you could use APPEND FROM ... TYPE SDF. The receiving table would need to have fields that match (in length) the fields within the line of text. If that's the case, the conversion is more or less automatic.

The only minor snag is that date fields are not always converted correctly. See the Help topic for APPEND FROM for details.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Manipulating long text lines, similar to fields in a table

(OP)
Hi mjcmkrsr and Chris,

Yep, I figured filetostr would be necessary, but couldn't figure out how we could perform something similar to table manipulations. I would still have to specify the exact location on each line where the specific pluses and minuses are, since that might be other occurrences of these characters that shouldn't be negated.



Chris and Mike,

Yes, I think that's the answer, to just split the table into multiple fields that would contain the full string, SDF append, and then do something like

REPLACE TEXTLIN1 WITH LEFT(TEXTLIN1, 100 ) + "+" + SUBSTR(TEXTLIN1,102) FOR LEFT(TEXTLIN1,3) = "BBB" .AND. SUBSTR(TEXTLIN1,101,1) = "-"

REPLACE TEXTLIN1 WITH LEFT(TEXTLIN1, 100 ) + "-" + SUBSTR(TEXTLIN1,102) FOR LEFT(TEXTLIN1,3) = "BBB" .AND. SUBSTR(TEXTLIN1,101,1) = "+"

(Not tested yet)

Obviously, I have to figure a way to avoid REPLACing lines with pluses that were already processed in the first REPLACE. But at least we have the basic idea.


Sounds good, thanks.


Regards,

RE: Manipulating long text lines, similar to fields in a table

If you know the numeric data has its sign at position 100 of a line, you can design your import DBF to read in the 99 characters before it into field1, then the + or - would be the first character of field2.

And, not only can you read in anything longer than 255 chars into as many char(254) fields as necessary but a COPY TO filename TYPE SDF also outputs the field concatenated without adding commas or other separators.

Chriss

RE: Manipulating long text lines, similar to fields in a table

(OP)
Chris,

Thanks, the Xbase designers thought of everything.

RE: Manipulating long text lines, similar to fields in a table

Hi,

or you use a three step approach

CODE -->

cTest = "hello - and + world"
? cTest
cTest =  CHRTRAN(cTest, "-", "*")
? cTest
cTest =  CHRTRAN(cTest, "+", "-")
? cTest
cTest =  CHRTRAN(cTest, "*", "+")
? cTest 

hth

MarK

RE: Manipulating long text lines, similar to fields in a table

(OP)
Yes, that's it. That's the final piece, an intermediary Search & Replace.

Thanks, mjcmkrsr.

RE: Manipulating long text lines, similar to fields in a table

With CHRTRAN(cFilecontent,'+-','-+') you also get all signs inverted without needing *. And the approach with * would also need to ensure there are no * in the original you then turn into +.

If you read in by APPEND, you have separate lines, no matter how the fields are composed, so you only ever have a single + you turn into a - or a single - you turn into a +, so no need for any trickery, here, especially not one that turns an initial * into +, when they should stay *.

Since you know the column of the signs that interests ou, I'd rather use the APPEND approach.

Chriss

RE: Manipulating long text lines, similar to fields in a table

Hi Chris,

You're right

Quote:


CHRTRAN(cFilecontent,'+-','-+')

Why did it not cross my mind?

MarK

RE: Manipulating long text lines, similar to fields in a table

It surely fits the example in the first post, as the positive numbers have a + sign (quite unusual).
Then you don't need APPEND. just a FILETOSTR() and STRTOFILE(). On the other side, the familiarity with the APPEND approach can be used and continued, just using multiple char fields and the SDF file type. Now you can pick whatever you prefer.

Chriss

RE: Manipulating long text lines, similar to fields in a table

(OP)
Hi Chris,

I'm already settled on doing APPEND, because then I can in effect manipulate the text file, the same way as a table.

With the FiletoStr, StrToFile, I'd end up with a memo field that contains the entire file.

I didn't know about CHRTRAN. That's a nice shortcut, thanks.

RE: Manipulating long text lines, similar to fields in a table

(OP)
All working perfectly, thanks everyone.


LIST SubStr( D2LINE1, 87, 1), ChrTran( SubStr( D2LINE1, 87, 1), "+-", "-+" ) FOR SUBSTR( D2LINE1, 72, 3 ) = "BBB" to file C:\DOWNLOAD\REPL.TXT

SUBSTR(d2line1,87,1) CHRTRAN(SUBSTR(d2line1,87,1),"+-","-+")
- +
+ -
+ -
+ -
+ -
...
...
...
+ -
+ -
+ -
- +
+ -
+ -
+ -
+ -
+ -
+ -
...
...
...
+ -
+ -
+ -



REPLACE D2LINE1 WITH LEFT(D2LINE1,86) + ChrTran( SubStr( D2LINE1, 87, 1), "+-", "-+" ) + SUBSTR(D2LINE1, 88, 166 ) FOR SUBSTR( D2LINE1, 72, 3 ) = "BBB"

*** Confirmed reversed signs in the table ***

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close