Oxygen Basic

Programming => Bugs & Feature Requests => Topic started by: kryton9 on June 20, 2011, 04:45:01 AM

Title: right and right$ not working
Post by: kryton9 on June 20, 2011, 04:45:01 AM
Code: OxygenBasic
  1. string cr$ =chr(13)+chr(10)
  2. string tab$ =chr(9)
  3.  
  4.  
  5. print "Hello first line" cr$ "a second line" cr$ "a third line" cr$ "a fourth line"
  6. print "this" tab$ "will be tabbed"
  7.  
  8. string s$ = "1234567890"
  9.  
  10. print left$ s$, 3
  11. print  left s$, 5
  12.  
  13. print mid$ s$, 4, 3
  14. print  mid s$, 4, 4
  15.  
  16. 'these give errors
  17. 'print right$ s$, 4
  18. 'print right s$, 4
Title: Re: right and right$ not working
Post by: Peter on June 20, 2011, 05:56:41 AM
 There is no right at all  :D
Title: Re: right and right$ not working
Post by: Charles Pegge on June 20, 2011, 08:25:31 AM

Right!

right is not part of the core functions as it is not used very often.

Try this instead:

print mid "12345678",-4,4

The negative offset means count from the right instead of from the left.

Charles


Title: Re: right and right$ not working
Post by: kryton9 on June 20, 2011, 12:05:36 PM
Thanks guys.
Title: Re: right and right$ not working
Post by: Charles Pegge on June 20, 2011, 12:31:36 PM

More trickery:

#define right(a,b) mid a,-b

print right ("12345678",4)


'My next posting of Oxygen will make the brackets optional.

Charles
Title: Re: right and right$ not working
Post by: Aurel on June 19, 2012, 02:04:15 AM
Hi..
If someone need Right there is function:
Code: [Select]
FUNCTION Right(getStr As String,rLen As Int) As String
String retStr
retStr = MID(getStr,-rLen)
RETURN retStr
END FUNCTION

by the way do we have function REPLACE$ ?
Title: Re: right and right$ not working
Post by: Charles Pegge on June 19, 2012, 04:13:25 AM
Hi Aurel,

Here is right in the newer macro form - it's 100% efficient.

macro right(s,i)
mid(s,-i)
end macro

Function for replace:

Code: OxygenBasic
  1.  
  2.   function replace(string t,w,r) as string
  3.   '=======================================
  4.  '
  5.  sys    a,b,lw,lr
  6.   string s=t
  7.   '
  8.  lw=len w
  9.   lr=len r
  10.   a=1
  11.   '  
  12.  do
  13.     a=instr a,s,w
  14.     if a=0 then exit do
  15.     s=left(s,a-1)+r+mid(s,a+lw)
  16.     a+=lr
  17.   end do
  18.   return s
  19.   end function
  20.  
  21.  

Charles
Title: Re: right and right$ not working
Post by: Peter on June 19, 2012, 04:39:51 AM
Why needs someone RIGHT$ ?  ???
Title: Re: right and right$ not working
Post by: Charles Pegge on June 19, 2012, 05:38:04 AM
Well I suppose having a left without a right might make one feel out of balance. It is surprising how rarely right()  is needed though.

Charles
Title: Re: right and right$ not working
Post by: Aurel on June 19, 2012, 05:58:28 AM
heh you right Charles and in many cases when translating from one source to oxygen source.
And sometimes is usefull even is rarely used.
Title: Re: right and right$ not working
Post by: Aurel on June 19, 2012, 06:52:45 AM
Just to test Replace function i made small program.
Is output correct?
Code: [Select]
#lookahead
string text
string r$,w$,out
text="oxygenbasic"
r$="Dev"
w$="basic"
out=""
 
out = Replace(text,w$,r$)
print out   'out -> oxygenDev


FUNCTION Replace(t as string,w as string,r as string) As String 
   '======================================= 
   ' 
   sys    a,b,lw,lr 
   string s=t 
   ' 
   lw=Len w 
   lr=Len r 
   a=1 
   '   
   DO 
     a=INSTR a,s,w 
    IF a=0 THEN EXIT DO 
     s=LEFT(s,a-1)+r+MID(s,a+lw) 
     a=a+lr 
   END DO 
   RETURN s 
END FUNCTION 
Title: Re: right and right$ not working
Post by: Aurel on June 19, 2012, 06:57:59 AM
It looks that work fine and here is both Right() & Replace()
Code: [Select]
#lookahead
string text
string r$,w$,out
text="oxygenbasic"
r$="Dev"
w$="basic"
out=""
 
'test Replace()
out = Replace(text,w$,r$)
print out   'out -> oxygenDev

'test Right()
out = Right(out,3)
print out  'out -> Dev


FUNCTION Replace(t as string,w as string,r as string) As String 
   '======================================= 
   ' 
   sys    a,b,lw,lr 
   string s=t 
   ' 
   lw=Len w 
   lr=Len r 
   a=1 
   '   
   DO 
     a=INSTR a,s,w 
    IF a=0 THEN EXIT DO 
     s=LEFT(s,a-1)+r+MID(s,a+lw) 
     a=a+lr 
   END DO 
   RETURN s 
END FUNCTION 

FUNCTION Right(getStr As String,rLen As Int) As String
String retStr
retStr = MID(getStr,-rLen)
Return retStr
END FUNCTION
Title: Re: right and right$ not working
Post by: Charles Pegge on June 19, 2012, 09:28:19 AM
That's fine, Aurel.

The only difference is that in the original replace, the strings are passed byval and in your functions the strings are passed byref, which is the default for basic-style prototypes.

Charles
Title: Re: right and right$ not working
Post by: Aurel on June 19, 2012, 12:13:45 PM
OK Charles.. ;)
Title: Re: right and right$ not working
Post by: Aurel on March 01, 2013, 02:07:06 PM
Here is the simple situation were Right$ function is used...
Code: [Select]
'save file
SUB doSave
INT hsize=0

dir=""
'char tx[32768]
'bstring tx
string ext=".o2bas"

filter="All Files (*.*)" + Chr(0) + "*.*" + Chr(0) + "Oxygen Files (*.o2bas)" + Chr(0) + "*.o2bas" + Chr(0)
title="Save File... "
hwnd=0
fName = FileDialog(dir,filter,title,0,1,"o2bas")
IF Right(fName,6)<>".o2bas"
fName=fName+ext
END IF
If fName="" then Return
print fname
hsize = SendMessage hsci, SCI_GETTEXTLENGTH, 0, 0
SendMessage hsci,SCI_GETTEXT,hsize+1,tx
PutFile fName,tx

END SUB
Title: Re: right and right$ not working
Post by: Peter on March 01, 2013, 03:05:11 PM
There is no RIGHT$ !
Title: Re: right and right$ not working
Post by: JRS on March 01, 2013, 07:20:15 PM
Quote
There is no RIGHTS !

Yep, we gave those up years ago because no one felt they were worth protecting. Do you want to see what justice has evolved into, spend some time at your local court house and be thankful your just an observer.





Title: Re: right and right$ not working
Post by: Peter on March 02, 2013, 01:04:26 AM
Quote
spend some time at your local court house

We have our courthouse  torn off.
It was eyesore in the eyes of the righteous people.
Title: Re: right and right$ not working
Post by: Aurel on September 01, 2013, 10:15:42 AM
Charles
In my expression evaluator macro right()
macro right(s,i)
mid(s,-i)
end macro

not work properly, i don't know why because macro default()

MACRO Default
DefWindowProc hwnd,wMsg,wParam,lParam
END MACRO


work fine  ;)

so i use Function Right()
Title: Re: right and right$ not working
Post by: Charles Pegge on September 01, 2013, 12:25:26 PM
Macros are vulnerable to operator grouping when passing an expressions

For instance

macro right(s,i)
mid(s,-i)
end macro

t="qwerty"
a=1
print "!"+right(t,2+a )+"?"   'result: !y?
print "!"+right(t,(2+a) )+"?" 'result: !rty?

solution: put brackets around i inside the macro

macro right(s,i)
mid(s,-(i))
end macro

t="qwerty"
a=1
print "!"+right(t,2+a )+"?"   'result: !rty?


Title: Re: right and right$ not working
Post by: Aurel on September 01, 2013, 01:04:31 PM
aha i see...
  ;)
Title: Re: right and right$ not working
Post by: JRS on September 01, 2013, 01:47:36 PM
It's easy to get lost on unpaved pathways. New highways aren't easy to build.