by showmyiq » Sun Mar 17, 2013 10:32 am
Markers are something really useful. In the previous chapter you saw how to push in the stack numeric values. Marker is non-numeric value. As you have already guessed by the name (marker), marker is useful for marking the stack. Remember the “Books” example in the first chapters? Elements in the stacks are like pile of books. You are not able to reach a lower book, if you don’t remove the upper ones. The marker is like a divider shoved between two books. The Marker is not book, just a divider between books. But let’s play with markers a little bit and I am sure you are going to fully appreciate the good profit of markers.
The command for creating marker is mark. It’s going to create a marker in the top of the stack (the newly created object, no matter what is the object – always going to the top of the stack). Ok, let’s go through the next example. Type the command: 1 2 mark 3 4 stack
You should receive this in the terminal:
4
3
--nostringval--
2
1
GS<5>
As you can see, between the elements 3 and 2 – a row exists. It states “--nostringval--”. This means that it’s not numeric value (otherwise it is going to be printed), it’s not string value ( otherwise it is going to be printed via ASCII characters), but it’s marker – a divider. You can see the console is stating that the stack has 5 elements now. It’s because the marker is counted for an element. Even the marker is not “a book”, it’s counted. You can have as many markers in the stack as you want. You can destroy a marker with the same command pop we have used of destroying a numeric value. In fact pop will destroy the top element of the stack, no matter what value it poses.
Now what we can do with the mark? Well we can count the elements in the stack, situated between the top of the stack and first marker. The command is counttomark.
Now don’t forget to clear the current elements of the stack, before proceeding with the next commands. If we type in the GS console (GS is abbreviation of GhostScript and if I use it in future, don’t be puzzled): 1 2 mark 3 4 counttomark stack
We are going to receive this:
2
4
3
--nostringval--
2
1
GS<6>
Let’s analyze the results. You can see that we have now 6 elements, instead of 5 and we have a new element with value 2 on the top of the stack. Can you guess from where the value 2 came? Yes, we have counted the elements between the top of the stack and the first marker – in our case that’s the elements 3 and 4, so the count is 1+1=2, and when we have successfully counted them, the marker created the value and pushed it on the top of the stack. Why counttomark command pushed the value on the top of the stack you may ask? Well, because PostScript is like that. You have a stack and you have elements. If you want to count the elements between the top of the stack and the marker, then it is supposed that you need the value, not just counting them. That’s why the results were saved and the only place they can be saved is the top of the stack. That’s the interesting and chaotic nature of this language – all is pushed in one place – creating obfuscation and cryptic feelings. You will be amazed in the next chapters, how we can write advanced programs on few rows only.
The last command I want to write down is the cleartomark command. It’s going to delete all the elements between the top of the stack and the first marker, with deleting the marker too.