Message Boards Message Boards

0
|
2241 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Print[] statement shows CSV contents in unexpected order?

Posted 1 year ago

Attachments:
POSTED BY: Richard Savage
2 Replies

Eric, I am somewhat new to Mathematica and want to thank you for your detailed (and excellent) response to my question.

The code you provided formats my notebook’s printed output in the way that I was naively trying to achieve with the code I mentioned in my original post. Also, I didn’t realize that just by inserting a "string" in a notebook, will cause it to be printed in the output when a notebook is evaluated. This feature will be useful for debugging.

Thanks again,

Rich

POSTED BY: Richard Savage
Posted 1 year ago

I doubt that the answer to the question you've posed is going to be helpful for what you're trying to achieve. Clarifying exactly what you're trying to do would help, but in the meantime, I'll make some assumptions and try to explain some things.

Print and FilePrint are examples of functions that have side effects. In fact, their sole purpose is to produce those side effects. The side effect is to print the results of evaluating the arguments into a new cell in the notebook, a cell of type Print. That might sound obvious, but it's actually somewhat special. Typical Mathematica expressions get evaluated, and when evaluation is complete, the results of the evaluation get displayed in a new Output cell. Print does not produce an output. So, you'll notice that you have no Output cell in your notebook.

The upshot is that you don't need to do anything special, like Print, to see the results of evaluating expressions. If you just evaluate this,

"Here 1 ------------------------------"

you'll see an Output cell with that string in it. Strings evaluate to themselves, so it appears as if nothing happened. You might notice that the formatting is ever so slightly different than what the Print produced.

You might think of Print as a sort of logging statement. It prints out on a different stream, so to speak, than where the evaluation output prints. I had never noticed before that subsequent FilePrint statements printed into the same Print cell, but apparently they do. I assume this is intended behavior, but even it it's not (if it's a bug), this isn't a blocker to what you're trying to do, or at least that's my assumption.

I assume what you're trying to do is to display these 7 strings in a column and in the same order they appear in the input. To do this, you just need to use functions that are designed to build up such structures, e.g. Grid or Column. Also, we need actual strings rather than Print statements. How do we get ahold of the actual strings? Well, as I showed above, you can just use a raw string for some of them. For the others, since the string is stored in a file, you need to use Import. So we can do this:

Column[
 {"Here 1 ------------------------------",
  Import["TF.csv"],
  "Here 2 ------------------------------",
  Import["TF.csv"],
  "Here 3 ------------------------------",
  Import["TF.csv"],
  "Here 4 ------------------------------"}]

Now, looking at this data, I assume that this is just fake data you used to illustrate your question, so I won't bother trying to clean this up. I will just say that this probably isn't the desired way to solve your problem. You'd probably use variables and string functions and maybe some other things.

To summarize, don't use Print as a display/formatting mechanism, use the appropriate built-in functions instead.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract