June 25, 2024, 07:26:18 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


passing string by ref

Started by TexasPete, February 07, 2009, 06:50:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TexasPete

I thought  I was doing by the book but evidently not.
def HTMLDOCUMENT [1224,2000]:ISTRNG
DEF Lines :int

gosub READTEXTFILE (HTMLDOCUMENT[] as byref,Lines as Int)

SUB READTEXTFILE((HTMLDOCUMENT[] as byref,Lines as Int)



return
end sub

I have tried several combinations of the above to no avail .

The book says to use simple brakets when passing a single dimensional array.

The return arrays is always messed up.

Thanks Texas Pet


aurelCB

Hey man what is this :
(HTMLDOCUMENT[]
This is empty array if i see right?

fasecero

February 07, 2009, 08:46:47 AM #2 Last Edit: February 07, 2009, 08:53:55 AM by fasecero
You should indicate in the definition and when using the function the size of the array since it is not of 1 dimension, is of 2.


def HTMLDOCUMENT [1224,2000]:istring
DEF Lines :int

gosub READTEXTFILE (HTMLDOCUMENT[1224,2000], Lines)

SUB READTEXTFILE (HTMLDOCUMENT[1224,2000] as string,Lines as Int)

return
end sub


Only when the function have 1 dimension you can not indicate the size


def SOMEARRAY[100]:istring
DEF Lines :int

gosub READSOMEARRAY(SOMEARRAY, Lines)

SUB READSOMEARRAY(SOMEARRAY[] as string,Lines as Int)

return
end sub

billhsln

I think your code:

def HTMLDOCUMENT [1224,2000]:ISTRNG
DEF Lines :int

gosub READTEXTFILE (HTMLDOCUMENT[] as byref,Lines as Int)

SUB READTEXTFILE((HTMLDOCUMENT[] as byref,Lines as Int)


what you actually are trying to do, may need to be defined as:

TYPE HOLD
DEF rec[1224]:ISTRING
ENDTYPE
DEF HTMLDOCUMENT[2000]:HOLD

HTMLDOCUMENT[lines].rec = iln


as to passing it to a subroutine, it would be passed byref.  Not sure how it would need to be defined in the subroutine.  Of course, you could just define it in the MAIN routine and allow it to be naturally passed to the subroutine.  This is allowed in BASIC.  Which means that you can reference the variable the same in the MAIN as the subroutine and not have to pass it.  The call to the subroutine would be as  gosub READSOMEARRAY() and SUB READSOMEARRY().

Bill
When all else fails, get a bigger hammer.

Ionic Wind Support Team

Ok first of all.

SUB READTEXTFILE((HTMLDOCUMENT[] as byref,Lines as Int)

Actually made me giggle at first   :D.   Pete, I said all strings are passed by reference by default, so that would mean the byref keyword isn't necessary.  The code is also wrong because byref isn't a type.  It is used for types that are passed "by value" such as:

sub mysub( blah as INT byref)

Arrays are also passed to a subroutine by reference, so again the keyword is not needed.  Read the users guide man, here is the appropriate section:

http://www.ionicwind.com/guides/emergence/language_topics_subroutines.htm

fasecero's second example is also wrong

Quote
def SOMEARRAY[100]:istring
DEF Lines :int

gosub READSOMEARRAY(SOMEARRAY, Lines)

SUB READSOMEARRAY(SOMEARRAY[] as string,Lines as Int)

return
end sub

Why?  Because an istring is treated as a normal string when only one dimension is specified.  The compiler will access that string in the subroutine wrong as it will think it is a string array, and not a dimensioned string.

Out of both recommendations I think Bill's is the one you should try and follow as it will lead to more understandable code for a beginner.  However with that said it depends on what you are trying to accomplish.  You keep using HTMLDOCUMENT as a variable name, so I can only guess you are trying to read a .htm or .html file for whatever reason.  Anyone that has worked with html knows that you can't expect it to have newlines  "\n" in it as it is very common for html generators to not use them, meaning trying to separate it into distinct lines can't be done with a simple READ statement. 

If you read the html specification (heady stuff):

http://www.w3.org/TR/html401/struct/text.html#line-breaks

line breaks (newlines) are considered as white space, so the document is in no way required to have them.  Which is why I mentioned to you in a different post that you have to expect html to be read as a long single line of text. 

Thus this:

Quote
<!DOCTYPE html>
<html>
  <head>
    <title>Hello HTML</title>
  </head>
  <body>
    <p>Hello World!!</p>
  </body>
</html>

and this

Quote
<!DOCTYPE html><html><head><title>Hello HTML</title></head><body><p>Hello World!!</p></body></html>

Are both valid html documents, the first one containing line breaks (newline characters).  A browser reads them as a sequence of tags, or markup, and isn't concerned whether or not they are separated. 

Paul.
Ionic Wind Support Team

fasecero

Quote
fasecero's second example is also wrong


My mistake, sorry. I forgot that:

def SOMEARRAY[number]:istring

it defines one single string of len " number " instead of an array. That second example would work with other types of variables, as int for example :)

TexasPete

Paul , I wrote the following.
I put the followin together to do some testing.
First when I define the HTMLDOCUMENT array. It appears that ebasic treats it as a two dimensional array.

If I assign HTMLDOCMENT[1] ="PRETTY BOLD" and then try to print "BOLD WORDS " , It will not print it to the screen in the for next loop below.

autodefine"on"
DECLARE READTEXTFILE(TEMP:STRING)
def HTMLDOCUMENT[2000,2000]:ISTRING
def Lines:int
def counter:int
OPENCONSOLE
HTMLDOCUMENT[2000,1]= "LINE ONE"
HTMLDOCUMENT[2000,2]= "LINE TWO"
HTMLDOCUMENT[2000,3]= "LINE THREE"
HTMLDOCUMENT[2000,4]= "LINE FOUR"
'gosub READTEXTFILE (HTMLDOCUMENT[2000,2000]as string ,Lines as int)

PRINT "bACK FROME "
FOR counter =1 to 4
print HTMLDOCUMENT[2000,counter]
next counter
'-------------------------------
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <>""
CLOSECONSOLE
END



SUB READTEXTFILE (HTMLDOCUMENT[2000,2000]as string,Lines as int)
FOR counter = 1 to 4
'print HTMLDOCUMENT[2000,counter]
next counter

return
ENDSUB

"What I lack in knowlege I will make up for it with tenacity"

Thanks
Texas Pete




LarryMc

I searched the forums using the words istring and arrays.

Among the hits I got was this one which I think will answer your question:

http://www.ionicwind.com/forums/index.php/topic,1650.msg15239.html#msg15239

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

Johnny

February 07, 2009, 05:10:25 PM #8 Last Edit: February 07, 2009, 05:12:47 PM by Johnny
I don't know exactly what the intention is of the whole program, neither why you like to pass the array variable through the sub's parameters.  :-[  But I suppose you have your reasons to do so!  :)
So when you really like to pass the istring array by its reference to the subroutine, then the following sample code can do that with a trick...
(It is probably not the only way to accomplish this, and maybe not even the simplest way to do this, but is does work this way!)  ;)

In the subroutine below I accept the reference from the array into a pointer (it's address points then to the memory where the array is stored), by calculating the relative position of the array's index inside the array you know then where in the memory the wanted piece of string can be found, you can then dereference the needed string from the array.
Anyway, you don't "need" to use the TempString variable here, so you can also print directly from the dereferenced pointer, but it looks a bit better to understand how it works this way.

I hope this helps a little bit...

Greetings,
Johnny



autodefine"off"
OPENCONSOLE

def LongStringArray[2000,200]:ISTRING
def Counter:int

LongStringArray[0,0]= "LINE ONE"
LongStringArray[0,1]= "LINE TWO"
LongStringArray[0,2]= "LINE THREE"
LongStringArray[0,3]= "LINE FOUR"

FOR Counter =0 to 4
PrintTextLine(LongStringArray,Counter)
next Counter
'-------------------------------
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <>""
CLOSECONSOLE
END
'-------------------------------

SUB PrintTextLine(LongStringArray2 as pointer,LineNumber as int)
def TempPointer:pointer
def TempString[2000]:istring
TempPointer = LongStringArray2 + LineNumber * 2000
TempString = #<string>TempPointer
print TempString

return
ENDSUB