March 28, 2024, 06:28:43 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Replacing a character in a string with a carriage return

Started by AdrianFox, April 30, 2012, 03:02:35 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AdrianFox

A simple question, but I need a push in the right direction!
   
I have a simple data file with the items delimited with a semi colon.  All I want to do is load the data into an edit box with the semicolon replaced by a carriage return, so the information appears on subsequent lines.

I haven't worked out how to do this, as I've tried simply reading the file a character at a time and replacing the semi colon when reading the file.  I thought I could do this by making my variable an istring of one character,  this doesn't work as the file is still read line by line.  I guess you can't read a file in this way!

How else could I approach this problem and replace each semicolon with a carriage return?  I'm not sure how to use replace$ when I don't know the position of each semicolon in the string being read.

def ln[1]:ISTRING
def ln2 as string
def buffer[32766]:ISTRING


'sub trying to achieve this

SUB ReadTheFile
ln2=chr$(13)

IF(OPENFILE(file1,"test.csv","R") = 0)

do



read(file1,ln)
if ln<>";"
buffer = buffer + ln
elseif buffer=buffer+ln2
endif

until EOF(file1)

endif
CLOSEFILE file1

           
SETCONTROLTEXT main,main_EDIT1,buffer

' output remains whole lines including the semicolon delimiter


RETURN 0
endsub



Adrian Fox

aurelCB

Adrian..
Is this your csv file:
Name1;Address1;Telephone1;
Name2;Address2;Telephone2;
Name3;Address3;Telephone3;
Name4;Address4;Telephone4 ;

Must be parsed line by line,use len of line then last charcter replace with CRLF= chr$(13)+chr$(10)

AdrianFox

Thanks.  I should have remembered that!   The problem with only occasional programming is you forget what you learnt before!
;)
Adrian Fox

aurelCB

Heh...
Yes ,i do programming almost every day but i still forget many things ::)

AdrianFox

UPDATE: Sorry, I hadn't appreciated that you can't REPLACE$ a single character with two or more characters without indicating a sufficient number, two in this case for the CR and LF.  The trouble with that is that if you have "name;address;telephone...."  when you replace the ; with CR etc you end up with
name
ddress
elephone etc.
Any ideas?





Still having problems with this.  I can get the line feed and CR after each line by adding chr$(13)+chr$(10) to the buffer variable in each 'pass',  but when I replace the semicolons which are in each line with these characters, I get a screen display as of a thick bar where the character has been replaced.  It won't operate the line feed, nor will /n as the replacement character either.

I'm using a multi line edit box.
I notice that when a file is loaded the new line character seems to be ignored.

buffer = buffer + ln+chr$(13)+chr$(10)
'    this works of course
'  but the following doesn't, even though the in line character ';'
is being replaced by the LFCR
IF MID$(ln, 11, 1) = ";"
REPLACE$ ln,11,1,chr$(13)+chr$(10)
endif


I should explain I am simply trying to display data formatted

Name;Address;Telephone;Notes

as
Name
Address
Telephone
Notes
Name2
Address2
Telephone2
Notes2

and so on.
Adrian Fox


LarryMc

This prints properly to a console window..
I use Fletchies's ctl.lib.
It can be done without his lib but it was just easier for me.

$include "ctl.inc"
file file1
istring ln[1000],left[1000],right[1000]
def buffer[32766]:ISTRING
ReadTheFile()
end

SUB ReadTheFile()
IF(OPENFILE(file1,getstartpath+"test.csv","R") = 0)
while (READ(file1,ln) = 0)
right=ln
while instr(right,";")
SplitTwo(right,";",left,right,0,1)
right= left+"\n"+right
wend
buffer += right
wend
CLOSEFILE file1
endif
print buffer
'SETCONTROLTEXT main,main_EDIT1,buffer
RETURN
endsub


LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

This is without Fletchie's lib.
file file1
istring ln[1000]
def buffer[32766]:ISTRING
ReadTheFile()
end

SUB ReadTheFile()
IF(OPENFILE(file1,getstartpath+"test.csv","R") = 0)
while (READ(file1,ln) = 0)
ln+="zz"
int pos = instr(ln,";")
while pos
left=left$(ln,pos-1)
right=mid$(ln, pos+1)
ln = left+"\n"+right
pos = instr(ln,";")
wend
ln=left$(ln,len(ln)-2)
buffer += ln
wend
CLOSEFILE file1
endif
print buffer
'SETCONTROLTEXT main,main_EDIT1,buffer
RETURN
endsub


LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ZeroDog

def win:window
openwindow win,0,0,200,200,@caption,0,"",&winproc
control win, @edit,"", 10,10, 150,150, @CTEDITMULTI, 1

ln="Name;Address;Telephone;Notes"

pos1=instr(ln,";",1)
ln2=mid$(ln,1,pos1-1)+"\n"
pos2=instr(ln,";",pos1+1)
ln2+=mid$(ln,pos1+1,pos2-pos1-1)+"\n"
pos3=instr(ln,";",pos2+1)
ln2+=mid$(ln,pos2+1,pos3-pos2-1)+"\n"
ln2+=mid$(ln,pos3+1)+"\n"

setcontroltext win,1, ln2

run=1
waituntil run=0
end

sub winproc(),int
select @class
case @idclosewindow
run=0
endselect
return 0
endsub

aurelCB

Uffff..... ;D
Larry and Zero beat me.... ;)
I have little bit different aproach by using two functions...
window win
File file1
String ln
istring buffer[4069]
int dpos[16]
string pStr[16]
int c
STRING CRLF = chr$(13)+chr$(10)
OPENWINDOW win,0,0,600,400,@MINBOX,0,"CVS",&main
SETWINDOWCOLOR win,RGB(220,220,226)
CONTROL win,@EDIT,"",10,32,440,280,0x50B010C4,1
CONTROL win,@BUTTON,"ReadF",460,32,80,24,0x50000000,2



waituntil win=0
end


SUB main
SELECT @MESSAGE
case @IDCLOSEWINDOW
closewindow win

Case @IDCONTROL
IF @controlid=2
ReadFile()
ENDIF

ENDSELECT
ENDSUB

SUB ReadFile



IF(OPENFILE(file1,GETSTARTPATH+"test.cvs","R") = 0)
Do
Read(file1,ln)
'count delimiters
c=Tally(ln,";")
'MESSAGEBOX 0,"Num:"+str$(c),"ok"
ParseArgs()

Until EOF(file1)

ENDIF
CLOSEFILE file1

ENDSUB


SUB Tally(STRING Main$,STRING Match$),INT
    DEF i,j,q,mlen,matchlen :INT
DEF t$:STRING

    mlen = LEN(Main$)
    matchlen = LEN(Match$)
    i = 1
    j = 0
    q = 0
    IF (mlen = 0) OR (matchlen = 0)
        RETURN j
    ENDIF
   
    WHILE 1

t$=MID$(Main$,i,matchlen)
IF t$ = CHR$(34) THEN q = q + 1
IF q=2 THEN q = 0
        IF t$ = Match$ AND q=0
           j = j + 1
'mem del$ position
dpos[j]=i
        ENDIF

           i = i + 1
        IF i > mlen
           Goto tbreak
        ENDIF
    WEND
Label tbreak
    RETURN j
ENDSUB

SUB ParseArgs
int k,i,j
string t$ = ln
i = 0
j = 1
k = 1
y=0
FOR i = 1 TO LEN(ln)
           IF i=dpos[k]
     k=k+1
                pStr[k] = MID$(t$,j,(i-j))
'MESSAGEBOX 0,pStr[k],""
buffer=buffer+pStr[k]+CRLF
SetControltext win,1,buffer
            j = i + 1
        ENDIF
    NEXT i
'+extract last argument
k=k+1
pStr[k] = MID$(t$,j,LEN(t$))
buffer=buffer+pStr[k]+CRLF
SetControltext win,1,buffer
RETURN
ENDSUB
           

AdrianFox

thanks Larry, AurelCB and Zerodog.   I've used Larry's solution (without Fletchie's lib) which does the job well for me, but I will work through the other solutions to help me understand how they work.
Thanks for the help.
:)
Adrian Fox