03-02-2015, 07:20 AM (This post was last modified: 03-02-2015 07:56 AM by Joe Horn.)
Post: #1
 Joe Horn Senior Member Posts: 1,902 Joined: Dec 2013
Here's an unexpected hidden feature. To pad a short string with spaces so that its total length is X characters, all it takes is:

REPLACE(string,X+1,"")

where "" is an empty string. If the input string is already X characters long (or longer), it is returned unchanged. Spaces are only appended if SIZE(string)<X.

Examples:

REPLACE("ABCDE",10+1,"") --> "ABCDE     " (ten characters long)
REPLACE("ABCDEFGHIJKL",10+1,") --> "ABCDEFGHIJKL" (unchanged)

Warning: Ridiculously large values for X makes Prime unhappy.

<0|ɸ|0>
-Joe-
03-03-2015, 07:32 AM (This post was last modified: 03-03-2015 07:35 AM by bobkrohn.)
Post: #2
 bobkrohn Member Posts: 142 Joined: Dec 2014
RE: Padding a string with spaces
Serendipity is a wonderful thing.
But you have to be able to recognize a good thing when it appears.
Good for you, Thanks

I now have a new Function in my Library.

Code:
 EXPORT PADR(MyStr,MyLen) BEGIN   REPLACE(MyStr,MyLen+1,"")▶MyStr; END;
03-03-2015, 08:02 AM
Post: #3
 Angus Member Posts: 212 Joined: Feb 2014
RE: Padding a string with spaces
Maybe it is important to note that I have to call

replace(). It is capitalized by the prime, but using REPLACE() directly won't work for me.
03-03-2015, 05:08 PM
Post: #4
 Han Senior Member Posts: 1,882 Joined: Dec 2013
RE: Padding a string with spaces
(03-03-2015 08:02 AM)Angus Wrote:  Maybe it is important to note that I have to call

replace(). It is capitalized by the prime, but using REPLACE() directly won't work for me.

Were you using REPLACE() in the CAS or Home view?

Graph 3D | QPI | SolveSys
03-03-2015, 09:34 PM
Post: #5
 bobkrohn Member Posts: 142 Joined: Dec 2014
RE: Padding a string with spaces
Home View (testing) and in Programming environment primarily.
I'm not using CAS so am tone deaf regarding subtle differences.
03-03-2015, 10:37 PM (This post was last modified: 03-03-2015 10:57 PM by Mark Hardman.)
Post: #6
 Mark Hardman Senior Member Posts: 525 Joined: Dec 2013
RE: Padding a string with spaces
(03-03-2015 08:02 AM)Angus Wrote:  Maybe it is important to note that I have to call

replace(). It is capitalized by the prime, but using REPLACE() directly won't work for me.

This only works in algebraic mode. Most likely you are in RPN mode. In that case you will need to enter the following on the stack:

"ABCDE"
11
""

Then enter:

REPLACE(3)
---
ETA: Alternately, you could enter 'REPLACE("ABCDE",10+1,"")' followed by EVAL to get the result in RPN mode.

Ceci n'est pas une signature.
03-03-2015, 10:40 PM (This post was last modified: 03-04-2015 04:22 AM by Mark Hardman.)
Post: #7
 Mark Hardman Senior Member Posts: 525 Joined: Dec 2013
RE: Padding a string with spaces
(03-02-2015 07:20 AM)Joe Horn Wrote:  Here's an unexpected hidden feature. To pad a short string with spaces so that its total length is X characters, all it takes is:

REPLACE(string,X+1,"")

It looks like this can be used to generate a left padded string by swapping the arguments:

REPLACE("",X+1,string)

For example:

Code:
 REPLACE("",10+1,"ABCDE") --> "          ABCDE"

ETD: This doesn't work as well as first though (see below).

Ceci n'est pas une signature.
03-04-2015, 02:20 AM (This post was last modified: 03-04-2015 02:21 AM by bobkrohn.)
Post: #8
 bobkrohn Member Posts: 142 Joined: Dec 2014
RE: Padding a string with spaces
(03-03-2015 10:40 PM)Mark Hardman Wrote:  It looks like this can be used to generate a left padded string by swapping the arguments:

REPLACE("",X+1,string)

Wished I thought of that. Good going!
I only hope these tidbits show up in the next version of the User Guide.
03-04-2015, 03:30 AM (This post was last modified: 03-04-2015 03:43 AM by bobkrohn.)
Post: #9
 bobkrohn Member Posts: 142 Joined: Dec 2014
RE: Padding a string with spaces

Appears that the Right Padding fits your String into a block of spaces that you designate.

However, the Left Padding just ADDS these spaces in front of the String.

I was trying to make a "do-all" function and that's why I noticed.
Unless of course I may be doing something wrong.
I'm pretty sure the Centering one is wrong but I quit when I noticed the above discrepancy.
Ideas anyone?
Maybe just subtract the length of your String from the number of desired Left pads?

Code:
 EXPORT PadLR(MyStr,MyLen,LorR)  BEGIN     // True  = -LorR = Left // False = +LorR = Right //     0 =       = Left and Right or centered //  REPLACE("",10+1,"ABCD") = "          ABCD" //  REPLACE("ABCD",10+1,"") = "ABCD      " IF LorR < 0 THEN   REPLACE("",MyLen + 1,MyStr)▶MyStr;  END; IF LorR > 0 THEN   REPLACE(MyStr,MyLen + 1,"")▶MyStr;  END; IF LorR == 0 THEN   REPLACE(MyStr,(MyLen) + 1,"")▶MyStr;    REPLACE("",(MyLen/2)+1,MyStr)▶MyStr; END; RETURN MyStr; END;
03-04-2015, 04:09 AM (This post was last modified: 03-04-2015 04:24 AM by Mark Hardman.)
Post: #10
 Mark Hardman Senior Member Posts: 525 Joined: Dec 2013
RE: Padding a string with spaces
(03-04-2015 03:30 AM)bobkrohn Wrote:  However, the Left Padding just ADDS these spaces in front of the String.

Maybe just subtract the length of your String from the number of desired Left pads?

Sorry about that. I've put strike-through on my previous post.

I agree that you will need to set the number of characters to pad. The problem is that the offset required doesn't seem consistent.

targetLength - stringLength - ?

I appears that this left pad "trick" is less useful than I thought.

Ceci n'est pas une signature.
03-04-2015, 05:43 AM (This post was last modified: 03-04-2015 06:05 AM by Angus.)
Post: #11
 Angus Member Posts: 212 Joined: Feb 2014
RE: Padding a string with spaces
fyi: I have home in RPN but tried in CAS. The error is "Error: Bad Argument Type" which I also get when in home/alg or home/textbook. replace() works. Interessting.
03-04-2015, 06:11 AM (This post was last modified: 03-04-2015 08:57 AM by bobkrohn.)
Post: #12
 bobkrohn Member Posts: 142 Joined: Dec 2014
RE: Padding a string with spaces
(03-04-2015 04:09 AM)Mark Hardman Wrote:  Sorry about that. I've put strike-through on my previous post.

I appears that this left pad "trick" is less useful than I thought.

Your exhibiting "thinking outside the box" which is a G-O-O-D thing.
I will still fiddle around with it as I hope others will too.
Have fun.

Here's what I came up with so far.

PHP Code:
EXPORT PadLR(MyStr,MyLen,LorR) BEGIN LOCAL i,s;  s:="";// True  = -LorR = Left// False = +LorR = Right//     0 =       = Centered//                 typ use Even MyLen IF LorR < 0 THEN  REPLACE("",MyLen + 1 - DIM(MyStr),MyStr)▶MyStr; END;IF LorR > 0 THEN  REPLACE(MyStr,MyLen + 1,"")▶MyStr; END;IF LorR == 0 THEN  FOR i FROM 1 TO (MyLen-DIM(MyStr))/2 DO    s := s + " ";   END;  s + MyStr + s ▶ MyStr;END;RETURN MyStr;END;
10-07-2015, 10:02 PM (This post was last modified: 10-07-2015 10:09 PM by StephenG1CMZ.)
Post: #13
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: Padding a string with spaces
I decided to have a go at this, based on the original syntax.
My changes include:
An input guard against negative/fractional parameters.
The input string is not modified.
In the centre case, a recursive call is used which I think makes the logic clearer, and avoids repeatedly adding spaces one by one. My implementation is probably less efficient, but provides a comparison for the previous logic.
Code:
    EXPORT ZPAD(ST,LNG,DD)  //ST PADDED NOT TRUNC  //LNG IS OUT MINLEN  //DD 1 R −1 L 0 C  BEGIN   LOCAL LL:=ABS(IP(LNG));//INGUARD   LOCAL SPC:=LL-DIM(ST);   LOCAL SPTMP:="";   CASE   IF DD>0 THEN //PAD R    RETURN REPLACE(ST,LL+1,"");//SYN:JOE HORN   END;   IF DD<0 THEN //PAD L     RETURN REPLACE("",SPC+1,ST);   END;    DEFAULT  //PAD C    SPTMP:=ZPAD("",SPC/2,1);//spaces    SPTMP:=SPTMP+ST+SPTMP;    RETURN      IFTE(DIM(SPTMP)<LL,SPTMP+" ",SPTMP);//extra space if odd   END;//CASE  END;  //LAYERED  EXPORT ZPADL(ST,LL)  BEGIN   RETURN ZPAD(ST,LL,−1);  END;    EXPORT ZPADR(ST,LL)  BEGIN   RETURN ZPAD(ST,LL,1);  END;  EXPORT ZPADC(ST,LL)  BEGIN   RETURN ZPAD(ST,LL,0);  END;  EXPORT ZS()  BEGIN    LOCAL ST:="";   LOCAL STIN:="ABC";   //PRINT();   ST:=ZPAD(STIN,9,1);   //PRINT(SIZE(STIN));//unchanged   //PRINT(SIZE(ST));   //PRINT("v"+ST+"V");//v just shows text limits   //WAIT;  END;

Stephen Lewkowicz (G1CMZ)
10-09-2015, 07:58 AM
Post: #14
 bobkrohn Member Posts: 142 Joined: Dec 2014
RE: Padding a string with spaces
I like it!
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)