Page 1 of 1

Left () and Right () with negative numbers

Posted: Fri Feb 22, 2019 10:24 am
by miskox
I would like to suggest to support negative numbers with Left.

Manual says:

Code: Select all

Result \$ = Left ( String$ , Length )
It would be nice to have an option to just specify a negative number which would tell how many characters are removed from the source string.

For example:

Code: Select all

result$=Left ("abcdef", -1)
would return

Code: Select all

abcde
(one character from the right removed).

This would probably mean shorter .exe because

Code: Select all

result$=Left ("abcdef", Len ("abcdef") - 1)
is much longer (which at the end produces longer .exe files).

Same could apply for Right.

Maybe some other functions could use the same system.

Just think about it and maybe implement it.

In the mean time maybe documentation could be updated to include the range of the Length. For example:

"Length: value specified must be an integer higher than zero." (of course rephrase this)

Saso

Re: Left () and Right () with negative numbers

Posted: Fri Feb 22, 2019 12:57 pm
by ar-s
I agree. That could be cool.

Re: Left () and Right () with negative numbers

Posted: Fri Feb 22, 2019 1:20 pm
by mestnyi
+1

Re: Left () and Right () with negative numbers

Posted: Fri Feb 22, 2019 1:28 pm
by RSBasic
+1

Re: Left () and Right () with negative numbers

Posted: Fri Feb 22, 2019 1:41 pm
by mk-soft
+1

Update. Only macros

Code: Select all

;-TOP
;-TOP

Macro PB(Function)
  Function
EndMacro

Macro Left(String, Lenght)
  PB(Left)(String, Lenght + (Bool(Lenght<0)*Len(String)))
EndMacro

Macro Right(String, Lenght)
  PB(Right)(String, Lenght + (Bool(Lenght<0)*Len(String)))
EndMacro

; --------

r1.s ="12345678"

Debug Left(r1, 2)
Debug Left(r1, -2)
Debug Right(r1, 2)
Debug Right(r1, -2)

Re: Left () and Right () with negative numbers

Posted: Fri Feb 22, 2019 2:06 pm
by Mijikai
+ 1

Re: Left () and Right () with negative numbers

Posted: Tue Jun 11, 2019 9:47 pm
by RobertRioja
This is a great idea which has already been implemented in other compilers. I hope it gets implemented in PB.

Re: Left () and Right () with negative numbers

Posted: Wed Jun 12, 2019 1:37 am
by Demivec
@mk-soft: I'm always glad to see code suggestions on how to obtain a solution through various means. Just a comment on your macro offering, they are useful as syntatic sugar but they will result in longer execution whether or not a negative number is used for length because the Len() will be calculated on every execution of the macro whether or not it is needed.

Re: Left () and Right () with negative numbers

Posted: Wed Jun 12, 2019 6:52 am
by Little John

Code: Select all

Macro LeftN (_string_, _length_)
   ; -- Left() with a negative number
   Left(_string_, Len(_string_) + (_length_))
EndMacro

Macro RightN (_string_, _length_)
   ; -- Right() with a negative number
   Mid(_string_, -(_length_) + 1)
EndMacro


; -- Demo
text$ = "12345678"

Debug LeftN (text$, -2)
Debug RightN(text$, -2)