Board Logo
« word functions »

Welcome Guest. Please Login or Register.
Jan 18th, 2018, 3:55pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: word functions  (Read 201 times)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx word functions
« Thread started on: Nov 27th, 2017, 10:34am »

hi,
here are some word functions:
Code:
ls$="abc bcd def ghi jkl mnp"

print lword$(ls$,3)
print cword$(ls$,3)
print rword$(ls$,3)
print lenword(ls$)
print midword$(ls$,2,3)
print revword$(ls$,5,2)


function lword$(st$,i)
   lword$=word$(wordsep$(st$,i),1,";")
end function
function cword$(st$,i)
   cword$=word$(wordsep$(st$,i),2,";")
end function
function rword$(st$,i)
   rword$=word$(wordsep$(st$,i),3,";")
end function
function lenword(st$)
   lenword=val(word$(wordsep$(st$,i),4,";"))
end function
function midword$(st$,i,j)
   tmp1$=rword$(st$,i-1)
   k=lenword(st$)
 if j<i then midword$=""
 if j>k then j=k
  for ii=1 to j
    tmp2$=tmp2$+chr$(32)+word$(tmp1$,ii)
  next
     midword$=trim$(tmp2$)
end function
function revword$(st$,i,j)
    k=lenword(st$)
 if i<j then revword$=""
 if (i+1)>k then i=k-1
    tmp1$=lword$(st$,i+1):k=lenword(tmp1$)
  for i=k to (k-j+1) step-1
     tmp2$=tmp2$+chr$(32)+word$(tmp1$,i)
  next
     revword$=trim$(tmp2$)
end function


function wordsep$(st$,i)
    k=0:ls$="":cs$="":rs$=""

 while word$(st$,k+1)<>""
     k=k+1
 wend

    if (i<1) or (i>k) then wordsep$=";;"

    if i=1 then ls$="":if i=k then rs$=""
  if i>1 then
     for j=1 to i-1
         ls$=ls$+chr$(32)+word$(st$,j)
     next
  end if
     cs$=word$(st$,i)
    if i<k then
      for j=i+1 to k
        rs$=rs$+chr$(32)+word$(st$,j)
      next
    end if
  wordsep$=trim$(ls$)+";"+trim$(cs$)+";"+trim$(rs$)+";"+str$(k)
end function


 
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: word functions
« Reply #1 on: Nov 27th, 2017, 11:28am »

Hi zzz,

Have you any applications planned for these functions?
User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: word functions
« Reply #2 on: Nov 27th, 2017, 11:47am »

(just my thoughts, B+)
wink

Nice work. But the application for it is more interesting wink
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: word functions
« Reply #3 on: Nov 30th, 2017, 10:03pm »

thank you tsh and B+
Quote:
Hi zzz,

Have you any applications planned for these functions?

here is an application which compares two text files and separates unique and common entries.
I used this to find what are the new files/folders added after installation of a particular software.
Code:
        output$="output.txt"
        input"enter filename 1 : ";fn1$
        input"enter filename 2 : ";fn2$
        lp=1
        [readfile]
        if lp=1 then fn$=fn1$
        if lp=2 then fn$=fn2$
        open fn$+".txt" for input as #1
      while eof(#1)=0
            input #1,t$
      wend
        if lp=1 then t1$=t$
        if lp=2 then t2$=t$
        close #1
        if lp<2 then lp=lp+1:goto [readfile]
        l1=lenword(t1$):l2=lenword(t2$)
        maxl=min(l1,l2):l$=str$(l1)+chr$(32)+str$(l2)
        [compare]
      for i=1 to l1
          for j=1 to l2
                if word$(t1$,i)= word$(t2$,j) then c$=c$+chr$(32)+word$(t2$,j):kwt=kwt+1:s=1:exit for
          next
            if s=1 then s=0:exit for
      next
        [mody]
        f1$=f1$+chr$(32)+lword$(t1$,i)
        t1$=rword$(t1$,i)
        t2$=lword$(t2$,j)+chr$(32)+rword$(t2$,j)
        l1=lenword(t1$):l2=l2-1
        'input"";a
        if kwt<>maxl  then goto [compare]
        [output]
        open output$ for append as #1
        print #1,"unique entries file1: ";lenword(f1$)
        print #1,"unique entries file2 ";lenword(t2$)
        print #1,"common entries";lenword(c$)
        print #1,"file 1"
        print #1,f1$
        print #1,"file 2"
        print #1,t2$
        print #1,"common"
        print #1,c$
        close #1
        print "f1 entries ",word$(l$,1),"f2 entries ",word$(l$,2)
        print "f1 unique entries ",lenword(f1$),"f2 unique entries ",lenword(t2$),"common entries ",lenword(c$)
        print "file 1"
      for i=1 to lenword(f1$)
            print word$(f1$,i)
      next
        print "file 2"
      for i=1 to lenword(t2$)
            print word$(t2$,i)
      next
        print "common"
      for i=1 to lenword(c$)
            print word$(c$,i)
      next
        '--------------------------------------
      function lword$(st$,i)
            l$=word$(wordsep$(st$,i),1,";")
            if i=1 then l$=word$(l$,2,";")
            lword$=trim$(l$)
      end function
      function cword$(st$,i)
            cword$=word$(wordsep$(st$,i),2,";")
      end function
      function rword$(st$,i)
            r$=word$(wordsep$(st$,i),3,";")
            if i=lenword(st$) then r$=word$(r$,2,";")
            rword$=trim$(r$)
      end function
      function lenword(st$)
            lenword=val(word$(wordsep$(st$,i),4,";"))
      end function
      function midword$(st$,i,j)
            tmp1$=rword$(st$,i-1)
            k=lenword(st$)
            if j<i then midword$=""
            if j>k then j=k
          for ii=1 to j
                tmp2$=tmp2$+chr$(32)+word$(tmp1$,ii)
          next
            midword$=trim$(tmp2$)
      end function
      function revword$(st$,i,j)
            k=lenword(st$)
            if i<j then revword$=""
            if (i+1)>k then i=k-1
            tmp1$=lword$(st$,i+1):k=lenword(tmp1$)
          for i=k to (k-j+1) step-1
                tmp2$=tmp2$+chr$(32)+word$(tmp1$,i)
          next
            revword$=trim$(tmp2$)
      end function
      function wordsep$(st$,i)
            k=0:ls$="":cs$="":rs$=""
          while word$(st$,k+1)<>""
                k=k+1
          wend
            if (i<1) or (i>k) then wordsep$=""
            if i=1 then ls$=" ":if i=k then rs$=" "
          if i>1 then
              for j=1 to i-1
                    ls$=ls$+chr$(32)+word$(st$,j)
              next
          end if
            cs$=word$(st$,i)
          if i<k then
              for j=i+1 to k
                    rs$=rs$+chr$(32)+word$(st$,j)
              next
          end if
            wordsep$=trim$(ls$)+";"+trim$(cs$)+";"+trim$(rs$)+";"+str$(k)
      end function

 

*getting files/folders list into text file is easy.For example
using bash script the command 'ls -r /usr/bin/ >abc.txt' sends the list of files and folders contained by /usr/bin/ to abc.txt.
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: word functions
« Reply #4 on: Dec 1st, 2017, 10:35am »

Have you actually tested this code on any files, specially longer than one line?

The FILES command can get you a list of files set in an array in JB. Check it out.

Also check how to load a file into a string.

Comparing two files is excellent practice AND useful!
User IP Logged

B+
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: word functions
« Reply #5 on: Dec 1st, 2017, 12:37pm »

yes B+ I have checked. The code works with new JB beta version. you too check and send your feed back /comments.

Edit1.If you want I will send text files containing containing 900+ entries contained in 900+ lines through gmail.
« Last Edit: Dec 1st, 2017, 12:41pm by zzz000abc » User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: word functions
« Reply #6 on: Dec 1st, 2017, 1:04pm »

I have a test for you: compare this file:
Code:
        open "zzz file read test.bas" for input as #1
      while eof(#1)=0
            input #1,t$
            'debug check, uncomment next line, the above line is split into 2
            'print t$ 
      wend
      close #1
print t$  'I get nothing as I expected!
print "done"

 


With this file:
Code:
        open "zzz file read test.bas" for input as #1
      while eof(#1)=0
            input #1,t$
            'debug check, uncomment next line, the above line is split into 2
            print t$   
      wend
      close #1
print t$  'I get nothing as I expected!
print "done"

 


Can it find the difference?

EDIT: some comments in code, I just figured out why they weren't showing up when I thought they should.

Note:
Code:
Input #1, t$  

treats commas in a line as a line separator.
« Last Edit: Dec 1st, 2017, 2:12pm by bplus » User IP Logged

B+
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: word functions
« Reply #7 on: Dec 3rd, 2017, 9:57pm »

thank you B+ for your feed back,
though above program in reply #3 serves my purpose (for the input file contains only folders/files names but no commas ) you extended my vision on file operations in JB.
I have started a new thread please check it.
http://justbasic.conforums.com/index.cgi?board=code&action=display&num=1512359309
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls