Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #793 > unrolled thread

Simple string padding function in Postscript

Started byDaniel Taylor <daniel.taylor@nzpost.co.nz>
First post2012-07-13 04:03 -0700
Last post2012-07-13 23:20 -0400
Articles 5 — 3 participants

Back to article view | Back to comp.lang.postscript


Contents

  Simple string padding function in Postscript Daniel Taylor <daniel.taylor@nzpost.co.nz> - 2012-07-13 04:03 -0700
    Re: Simple string padding function in Postscript tlvp <mPiOsUcB.EtLlLvEp@att.net> - 2012-07-13 14:42 -0400
    Re: Simple string padding function in Postscript Helge Blischke <h.blischke@acm.org> - 2012-07-13 21:19 +0200
      Re: Simple string padding function in Postscript Daniel Taylor <daniel.taylor@nzpost.co.nz> - 2012-07-13 14:37 -0700
      Re: Simple string padding function in Postscript tlvp <mPiOsUcB.EtLlLvEp@att.net> - 2012-07-13 23:20 -0400

#793 — Simple string padding function in Postscript

FromDaniel Taylor <daniel.taylor@nzpost.co.nz>
Date2012-07-13 04:03 -0700
SubjectSimple string padding function in Postscript
Message-ID<362f3578-16b0-4f21-994f-89412506d3b9@googlegroups.com>
I want to write a simple function to convert and integer into a string 6 characters in length, padding with zeros as necessary. The function add the necessary zeros to the front of the string to make the overall length 6. Ideally I could pass the function the integer, and the length I want the string to be.

For example, if I input '1' I want the function to return '000001'

or if input '123' I want the function to return '000123'.

I've only recently started learning Postscript programming and this is one application I haven't been able to solve.

[toc] | [next] | [standalone]


#794

Fromtlvp <mPiOsUcB.EtLlLvEp@att.net>
Date2012-07-13 14:42 -0400
Message-ID<1qxmh30m06v52$.gqe0riwsphq0.dlg@40tude.net>
In reply to#793
On Fri, 13 Jul 2012 04:03:45 -0700 (PDT), Daniel Taylor wrote:

> I want to write a simple function to convert and integer into a string 6 characters in length, padding with zeros as necessary. The function add the necessary zeros to the front of the string to make the overall length 6. Ideally I could pass the function the integer, and the length I want the string to be.
> 
> For example, if I input '1' I want the function to return '000001'
> 
> or if input '123' I want the function to return '000123'.
> 
> I've only recently started learning Postscript programming and this is one application I haven't been able to solve.

That's less a problem in PostScript than a problem in algorithmic
arithmetic.

Your desired left-most digit is the integer value, before remainder, of
dividing your given number by 100,000.
Your next desired digit is the integer value, before remainder, of dividing
the *remainder* from the previous division by 10,000.
Your next ... " ... " ... by 1,000.  % by ... " ... " ... I mean 'ditto' .
And likewise, for 100, 10, and 1, respectively.

That's the algorithmic arithmetic. How to accomplish those 6 steps in
PostScript -- and perhaps how to implement them in a loop -- I leave as a
worthwhile PostScript exercise for you :-) .

Cheers, -- tlvp
-- 
Avant de repondre, jeter la poubelle, SVP.

[toc] | [prev] | [next] | [standalone]


#795

FromHelge Blischke <h.blischke@acm.org>
Date2012-07-13 21:19 +0200
Message-ID<a6baluF1v5U1@mid.individual.net>
In reply to#793
Daniel Taylor wrote:

> I want to write a simple function to convert and integer into a string 6
> characters in length, padding with zeros as necessary. The function add
> the necessary zeros to the front of the string to make the overall length
> 6. Ideally I could pass the function the integer, and the length I want
> the string to be.
> 
> For example, if I input '1' I want the function to return '000001'
> 
> or if input '123' I want the function to return '000123'.
> 
> I've only recently started learning Postscript programming and this is one
> application I haven't been able to solve.

A fairly simple solution (without algorithmic arithmetic) is by the 
following procedure:

/bindec         % <integer> bindec <string_of_length_6>
{
        1000000 add 7 string cvs 1 6 getinterval
}bind def


Helge

[toc] | [prev] | [next] | [standalone]


#796

FromDaniel Taylor <daniel.taylor@nzpost.co.nz>
Date2012-07-13 14:37 -0700
Message-ID<169114e5-8743-416e-a2fa-c1a43ae30a87@googlegroups.com>
In reply to#795
On Saturday, 14 July 2012 07:19:24 UTC+12, Helge Blischke  wrote:
> Daniel Taylor wrote:
> 
> &gt; I want to write a simple function to convert and integer into a string 6
> &gt; characters in length, padding with zeros as necessary. The function add
> &gt; the necessary zeros to the front of the string to make the overall length
> &gt; 6. Ideally I could pass the function the integer, and the length I want
> &gt; the string to be.
> &gt; 
> &gt; For example, if I input &#39;1&#39; I want the function to return &#39;000001&#39;
> &gt; 
> &gt; or if input &#39;123&#39; I want the function to return &#39;000123&#39;.
> &gt; 
> &gt; I&#39;ve only recently started learning Postscript programming and this is one
> &gt; application I haven&#39;t been able to solve.
> 
> A fairly simple solution (without algorithmic arithmetic) is by the 
> following procedure:
> 
> /bindec         % &lt;integer&gt; bindec &lt;string_of_length_6&gt;
> {
>         1000000 add 7 string cvs 1 6 getinterval
> }bind def
> 
> 
> Helge

That's a fantastic help thank you. Very simple and very easy for me to get my head around.

Regards

Daniel

[toc] | [prev] | [next] | [standalone]


#797

Fromtlvp <mPiOsUcB.EtLlLvEp@att.net>
Date2012-07-13 23:20 -0400
Message-ID<1loqhprsja8b7$.6yjpiw88z11i.dlg@40tude.net>
In reply to#795
On Fri, 13 Jul 2012 21:19:24 +0200, Helge Blischke wrote:

> Daniel Taylor wrote:
> 
>> I want to write a simple function to convert and integer into a string 6
>> characters in length, padding with zeros as necessary. The function add
>> the necessary zeros to the front of the string to make the overall length
>> 6. Ideally I could pass the function the integer, and the length I want
>> the string to be.
>> 
>> For example, if I input '1' I want the function to return '000001'
>> 
>> or if input '123' I want the function to return '000123'.
>> 
>> I've only recently started learning Postscript programming and this is one
>> application I haven't been able to solve.
> 
> A fairly simple solution (without algorithmic arithmetic) is by the 
> following procedure:
> 
> /bindec         % <integer> bindec <string_of_length_6>
> {
>         1000000 add 7 string cvs 1 6 getinterval
> }bind def
> 
> 
> Helge

Elegant, Helge :-) . Alas, each of us is congenitally predisposed to see
each problem as the sort of nail our own unique hammers can deal with, and
thereupon we deal with it accordingly. In this instance, your hammer is by
far the more appropriate, as is your perspective on the problem :-) .

Cheers, -- tlvp
-- 
Avant de repondre, jeter la poubelle, SVP.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.postscript


csiph-web