Jump to content

Parse a text field when there's no delimiting

This topic is 2831 days old. Please don't post here. Open a new topic instead.

Recommended Posts

G'morning, all:


Talk about goofy: one of our users receives a text file on a regular basis where there's no delimiting--it's a fixed string around 4K-long filled with different field data and blank spaces. As I look at it, the data is in block lengths so the first block of data is 50 characters long (including spaces, if any), the second block is 75 characters long (including spaces, if any), etc.


So, what I want to do is build a script to import the file into FileMaker (into a global field), then strip the data from the global field into separate text fields. My question is, what's the best practice way of doing that? I was thinking that I'd use a loop to cut the left-most "x" characters from the global field then paste the data into its respective text field and repeat as necessary until there's no data left in the field; it's destructive to the contents of the global field but ithat's no biggie since its contents would be temporary, anyway. However, if I go this route, how would I code the script step to copy just "x" number of characters then delete them?


I better put the coffee pot on--this is going to be a long day. :|  TIA for your help!

Link to comment
Share on other sites

Your description of the incoming format is not clear enough. Does it contain more than one record? If yes, what (if anything) separates the records?


In any case, using cut and paste is hardly the optimal solution; just use:

Set Field [ TargetTable::Field2 ; Middle ( $chunk ; 51 ; 75 ) ] 

(as an example for the second block)


or perhaps more usefully:

Set Field [ TargetTable::Field2 ; Trim ( Middle ( $chunk ; 51 ; 75 ) ) ]

where $chunk is a variable containing either the entire imported text or only a part of it representing one record - depending on your answer to my first question.

Link to comment
Share on other sites

if you build something and the incoming format changes by one char it blows up


True, but that will happen with any interface you build for importing or exporting: if "they" change their format, it will stop working. In the normal course of things, these formats are not supposed to change, precisely for this reason (and if anyone is still using a fixed-length format, they're not very likely to change it now anyway).

Link to comment
Share on other sites

Sorry, I forgot to include that--a carriage return separates each record.


I sanitized the file and it has two records--it's attached. (The character count differs because I didn't match block lengths when substituting data, but you get the idea.)


Link to comment
Share on other sites

a carriage return separates each record.


In such case I'd suggest you do it along these lines:


Set Variable [ $allRecords; SourceTable::gText ]
Go to Layout [ TargetTable]
 Set Variable [ $i; $i + 1 ]
 Exit Loop If [ $i > ValueCount ( $allRecords) ]
 Set Variable [ $record; GetValue ( $allRecords ; $i ) ]
 New Record/Request
 Set Field [ TargetTable::Field1; Trim ( Middle ( $record ; 1 ; 50 ) ) ]
 Set Field [ TargetTable::Field2; Trim ( Middle ( $record ; 51 ; 75 ) ) ]
 # ... set more fields ...
End Loop
Commit Records/Requests
Link to comment
Share on other sites

This topic is 2831 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.