Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
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
Solved! Go to Solution.
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
@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
The challenge is accepted.
Attached is a Prime4 file.
Success!
Luc
@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:
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
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