Author Topic: right and right$ not working  (Read 6818 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
right and right$ not working
« 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

Peter

  • Guest
Re: right and right$ not working
« Reply #1 on: June 20, 2011, 05:56:41 AM »
 There is no right at all  :D
« Last Edit: June 20, 2011, 06:00:17 AM by peter »

Charles Pegge

  • Guest
Re: right and right$ not working
« Reply #2 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



kryton9

  • Guest
Re: right and right$ not working
« Reply #3 on: June 20, 2011, 12:05:36 PM »
Thanks guys.

Charles Pegge

  • Guest
Re: right and right$ not working
« Reply #4 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

Aurel

  • Guest
Re: right and right$ not working
« Reply #5 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$ ?

Charles Pegge

  • Guest
Re: right and right$ not working
« Reply #6 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
« Last Edit: June 19, 2012, 04:21:00 AM by Charles Pegge »

Peter

  • Guest
Re: right and right$ not working
« Reply #7 on: June 19, 2012, 04:39:51 AM »
Why needs someone RIGHT$ ?  ???
« Last Edit: June 19, 2012, 08:33:14 AM by peter »

Charles Pegge

  • Guest
Re: right and right$ not working
« Reply #8 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

Aurel

  • Guest
Re: right and right$ not working
« Reply #9 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.
« Last Edit: June 19, 2012, 06:05:07 AM by Aurel »

Aurel

  • Guest
Re: right and right$ not working
« Reply #10 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 

Aurel

  • Guest
Re: right and right$ not working
« Reply #11 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

Charles Pegge

  • Guest
Re: right and right$ not working
« Reply #12 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

Aurel

  • Guest
Re: right and right$ not working
« Reply #13 on: June 19, 2012, 12:13:45 PM »
OK Charles.. ;)

Aurel

  • Guest
Re: right and right$ not working
« Reply #14 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