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.
This is the useful type stuff I love to read about.
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;
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 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?
Home View (testing) and in Programming environment primarily.
I'm not using CAS so am tone deaf regarding subtle differences.
(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.
(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).
(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.
Just noticed some thing about the "Left Padding".
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 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.
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 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.
What's to be sorry about!?
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;
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;