cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Importing a text file to a vector of lines, whilst retaining all leading and trailing spaces

StuartBruff
23-Emerald III

Importing a text file to a vector of lines, whilst retaining all leading and trailing spaces

Can any of the "READ" functions in Mathcad Prime 7 import a text file to a vector of lines, whilst retaining all leading and trailing spaces?

 

I can easily write a short function using READBIN to get the job done, but I'd rather just use one of the READ functions. 

 

Unfortunately,  I'm having a minor brain meltdown due to the extreme temperatures and humidity we're experiencing in my part of  Scotland (25 C and 64% RH), and I've gone option blind.   Everything I've tried ends up losing leading or trailing spaces.

 

Cheers,

 

Stuart

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:StuartBruff)

The challenge is accepted.

 

Attached is a Prime4 file.

 

Success!

Luc

View solution in original post

5 REPLIES 5
LucMeekes
23-Emerald III
(To:StuartBruff)

The two obvious options are READTEXT and READFILE.

Unfortunately both are (apparently) targeted at reading numeric data from a (more or less formatted) file into a matrix.

Guess you'll have to program.

 

Success!
Luc

 

StuartBruff
23-Emerald III
(To:LucMeekes)


@LucMeekes wrote:

The two obvious options are READTEXT and READFILE.

Unfortunately both are (apparently) targeted at reading numeric data from a (more or less formatted) file into a matrix.


Sadly, that's what I discovered.  I was hoping there was some combination of column, width, etc options that would read in a whole line.

 


Guess you'll have to program.

Yes, already done.   But I've only got it for another 14 days and then the magic clock turns my Prime licence into an Express licence.

 

If only Mathcad did proper recursion, like a real functional language is supposed to, with local recursion, partial application, mutual recursion, resource-limited recursion depth, and tail-call optimization.  Of course, it used to have the first two.  I'm not sure whether recursive functions were just forgotten about or ignored, but I'm sure that partial application was deliberately removed because it confused someone.

 

Still, I suppose a divide-and-rejoin algorithm might work under Express.

 

Stuart

LucMeekes
23-Emerald III
(To:StuartBruff)

The challenge is accepted.

 

Attached is a Prime4 file.

 

Success!

Luc

StuartBruff
23-Emerald III
(To:LucMeekes)


@LucMeekes wrote:

The challenge is accepted.

 

Attached is a Prime4 file.

 

Success!

Luc


 

Nice Luc ... and almost success, I think.   There are two problems that I think the functions have: 

 

  • READLINES doesn't seem to like trailing line terminators.
  • It hits Prime's recursion after about 4500 lines ... not a lot in these days of Big Data.  That's why I suggested a divide-and-rejoin algorithm - they can be surprisingly fast, as well.

 

If you don't already know about it, you might be interested in the undocumented Prime function fold.  It's a left fold function and doesn't seem to have the same problem with recursion depth.

 

I've attached a Mathcad Prime 7 worksheet that presents my findings and introduces the fold function.

 

Cheers,

 

Stuart

 

LucMeekes
23-Emerald III
(To:StuartBruff)

Thanks Stuart.

I was aware that the READLINES and WRITELINES functions weren't universally capable. But at least for a subset of text files they would work. Potential problems I had thought about are: empty lines/strings, CRLF vs only LF, and the recursion limit that I also had dealt with when creating the histogram() function (see Joke) and I believe I had circumvented it there. I didn't have/take the time to make them perfect. Thank you for doing that, and the fold() function is certainly something to take a closer look at. I wonder if I can use it to create a better version of rkfixed().

 

For a broader audience I attach a Prime4 copy of the mcdx file you attached.

 

Luc

Announcements

Top Tags