Here is my export script to txt (in attachment)
The problem is that I can't rid of «magic number» 4 in $if section.
It's not cross-functional. I think I need another $if there, but can't figure out what $if.
Hope I described the problem clearly.