Page 1 of 1

Getting length of a field in characters within a structure

Posted: Tue Jun 05, 2007 2:53 pm
by Prof
Hello folks.

I am having a problem and would be grateful for any help on this issue.

I have a structure of fixed length strings that I am putting inside an array. My problem is that I cannot find a way to get the length of a specific field within the structure once it has been created.

I am trying to create a random access file of records and I am using LSET to 'padd out' the data to make it uniform if a string is shorter than the definition within the structure.

Here is a little code to help explain things....

Code: Select all

 Structure Student
    Fornames.s{30}
    Surname.s{25} 
    DOB_DD.s{2}
    DOB_MM.s{2}
    DOB_YY.s{2}
  EndStructure
  
  Dim Student_Array.Student(100)
 
  Student_Array(0)\Fornames.s = "Andrew"+LSet(Student_Array(0)\Fornames.s,30)
The last line shows the maximum length in characters (30) that the Fornames.s field should be padded out to with the LSET command. Obviously, I dont want to use the 30 at all and would just like to retrieve the previously defined lenght and put it in its place.

I have had a good look around and can't seem to find a command to get the length in characters of a specific field. I can get the length of the whole structure but not a specific field.

Any help on this would be great.

Many thanks, Prof.

Posted: Tue Jun 05, 2007 3:01 pm
by Thalius

Code: Select all

Student_Array(0)\Fornames.s = "Andrew"+LSet(Student_Array(0)\Fornames.s,Len(Student_Array(0)\Fornames.s)) 
should do the Trick.

btw. for the DD MM YY you could instead use a single LONG and store the date as Date-Number.

Code: Select all

debug date()
Thalius

Posted: Tue Jun 05, 2007 3:11 pm
by Prof
Thanks Thalius....

I already sussed it just after I posted it. I tried the LEN command earlier as you suggest above but I couldn't get it to work.

Instead I used the SizeOf function which appears to work on the specific fields as well as the whole structure

Code: Select all

Student_Array(0)\Fornames.s = "Andrew"+LSet(Student_Array(0)\Fornames.s, SizeOf(Student\Fornames.s))
I dont know why the LEN function didn't work because thats the first thing that I tried :/

Thanks for your reply anyway.

Prof.

Posted: Tue Jun 05, 2007 4:34 pm
by Fluid Byte
If you know the order of the elements you could do it like this:

Code: Select all

Structure Student
	Fornames.s{30}
	Surname.s{25}
	DOB_DD.s{2}
	DOB_MM.s{2}
	DOB_YY.s{2}
EndStructure 
  
LenSurname = OffsetOf(Student\DOB_DD) - OffsetOf(Student\Surname)

Debug LenSurname

Posted: Tue Jun 05, 2007 5:59 pm
by Fred
The Sizeof(Structure\Field) is the prefered way for that. Len() won't work here because it will stop to the first null char encountered.

Posted: Tue Jun 05, 2007 6:31 pm
by Trond
I am trying to create a random access file of records and I am using LSET to 'padd out' the data to make it uniform if a string is shorter than the definition within the structure.
Why do you need to do that? You can just use WriteStringN() to use newline as a separator. The file will take less space as well.

Posted: Tue Jun 05, 2007 6:43 pm
by Prof
Hello Trond.

I am sorry if I didn't explain things clearly. I am using the FileSeek function to move through a file in a random access kind of way and it would make things difficult for me if I put each field on its own line in a file. (CR's to worry about)

I don't need to use LEN at all because the structure and its space has already been defined so all I have to do is 'padd out' the fields until they reach their pre-defined length. I can get this info from the SizeOf function.

As Fred says, the SizeOf function is the preferred way and probably the best way to do it.

Thanks for all your help chaps!

Prof.

Posted: Wed Jun 06, 2007 2:53 pm
by Thalius
Oh misunderstood then ;)

SizeOF() yeah ! - too much PHP lately .. damn cus(s)tomers.. ;)

Glad it works now..

Thalius