padding integers in bc
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > padding integers in bc




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    padding integers in bc  
Robert Katz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-27-06 06:29 AM

I'm trying to define a bc function that will pad an integer with
leading 0s.

For example, I want pad(57,4) -> 0057; pad(593,2) -> 593

This definition of pad puts the correct number of leading 0s, but it
also adds a trailing 0.  I don't know why.

####
define pad(x,y){
auto w
w=y-length(x)
while(w>0){print"0";w-=1}
print x
}
####

--
Regards,

---Robert





[ Post a follow-up to this message ]



    Re: padding integers in bc  
loic-dev@gmx.net


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-27-06 06:29 AM

Hello Robert,

> I'm trying to define a bc function that will pad an integer with
> leading 0s.
>
> For example, I want pad(57,4) -> 0057; pad(593,2) -> 593
>
> This definition of pad puts the correct number of leading 0s, but it
> also adds a trailing 0.  I don't know why.
>
> ####
> define pad(x,y){
> auto w
> w=y-length(x)
> while(w>0){print"0";w-=1}
> print x
> }
> ####

The trailing '0' you are seeing is in fact the return value of the
pad() function. Doing something like:
ret = pad(57,4)

should produce the 'correct' output.

HTH,
Loic.






[ Post a follow-up to this message ]



    Re: padding integers in bc  
Robert Katz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-27-06 06:18 PM

loic-dev@gmx.net wrote:
> Hello Robert,
> 
>
> The trailing '0' you are seeing is in fact the return value of the
> pad() function. Doing something like:
> ret = pad(57,4)
>
> should produce the 'correct' output.
>
> HTH,
> Loic.
>

This worked:

define pad(x,y){
auto w
w=y-length(x)
while(w>0)print"0";w-=1}
return x
}

--
Regards,

---Robert





[ Post a follow-up to this message ]



    Re: padding integers in bc  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-27-06 06:18 PM

Robert Katz wrote:
> loic-dev@gmx.net wrote:
> 
>
> This worked:

Not quite.

>
>     define pad(x,y){
>     auto w
>     w=y-length(x)
>     while(w>0)print"0";w-=1}

while(w>0){print"0";w-=1}

>     return x
>     }
>

Or using...  while(w-->0)print"0"

Janis





[ Post a follow-up to this message ]



    Re: padding integers in bc  
Robert Katz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 12:23 AM

Janis Papanagnou wrote:
> Robert Katz wrote: 
>
> Not quite.

Sure it did.  See my previous message, and you'll see that the 'This
worked:' comment refers to the next routine, not the previous one,
which worked ... well, Not quite.

> 
>
>       while(w>0){print"0";w-=1}
> 
>
> Or using...  while(w-->0)print"0"
>


--
Regards,

---Robert





[ Post a follow-up to this message ]



    Re: padding integers in bc  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 12:23 AM

Robert Katz wrote:
> Janis Papanagnou wrote:
> 
>
>
> Sure it did.

The routine you gave below gives a syntax error because you have
omitted an opening curly brace after while(w>0). I've fixed your
error in my reply (as you can see by the indentation level) below.

> See my previous message, and you'll see that the 'This
> worked:' comment refers to the next routine, not the previous one, which
> worked ... well, Not quite.
> 
>
>





[ Post a follow-up to this message ]



    Re: padding integers in bc  
Robert Katz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:32 AM

Janis Papanagnou wrote:
> Robert Katz wrote: 
>
> The routine you gave below gives a syntax error because you have
> omitted an opening curly brace after while(w>0). I've fixed your
> error in my reply (as you can see by the indentation level) below.

Dang it!  You got me there.  Not only would it not quite work, it
wouldn't work at all.[vbcol=seagreen]
> 

--
Regards,

---Robert





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:33 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register