Message Boards Message Boards

0
|
2350 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How can I save a notebook and reload with same line numbers?

When I work on a notebook, as I edit equations, the line numbers increment, and some lines are deleted or I delete them. But I often reference line numbers in later calculations. That works as long as the notebook is open, but when I save it and reload it, the numbers change when Mathematica does a recalculation. This messes up the recalculation. Is there any way to get Mathematica to keep the numbers as they are when the notebook is closed?

POSTED BY: Thomas Fowler
3 Replies
Posted 1 year ago

I hesitate to reply, because you'll probably be offended, but this is just too much to let lie. First off, there are no line numbers in the traditional sense--it's just Out and In. So, your mental mode of what's even happening here is probably wrong.

Not only is it a bad idea to try to keep line numbers consistent between kernel sessions, it's a bad idea even within a single session. No one writes Mathematica code by referencing Out directly unless they're doing something very ad hoc or exceptional. You will find no robust packages/libraries that do this. You will find no examples from experienced Mathematica programmers that do this (unless, again, it's something extremely unusual). The whole idea is just a nightmare.

As for handling "programs reloaded from disc", Mathematica does this just fine. I can't imagine what you think "line numbers" have to do with this. Mathematica also handles "deletions" just fine. It also handles additions and edits. None of this has anything to do with "line numbers".

You've adopted a very particular and peculiar programming habit, and since it's your habit, you don't have an objective frame of reference from which to see how bad it truly is. You can read entire books on programming with Mathematica that will never reference Out or In explicitly. I think you'd be hard pressed to find any code examples on this site or on any other Mathematica related site where explicit references to Out or In are used. I've been writing Mathematica code for over 25 years, and I have literally never depended explicitly on "line numbers". One of the most frustrating things about BASIC was that it required you to explicitly referenc line numbers--it was a relief to learn other languages that didn't have this restriction. It boggles the mind to learn that someone wants to revive that pattern.

But, you must find your own path. So, if you are truly desperate for this kind of "line number" based programming pattern, you should look into $Line. Explicitly manipulating it will make your code truly ugly, but that's your own affair.

POSTED BY: Eric Rimbey

Yes I know that I can do that, but assigning a name to every line is very inefficient and inconvenient. Even the old Derive program could easily handle line deletions and other renumberings, and programs reloaded from disc files executed perfectly. This is basic stuff. Wolfram needs to fix this problem.

POSTED BY: Thomas Fowler

Line numbers are tracked by the kernel. When the kernel quits the line numbers are lost. Explicitly referring to line numbers in a notebook is a bad idea. It is much better to assign meaningful names to the result of an evaluation and use those names in subsequent computations. The resulting code is much easier to understand, debug and maintain.

POSTED BY: Rohit Namjoshi
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