Getting length of a field in characters within a structure

Just starting out? Need help? Post your questions and find answers here.
Prof
User
User
Posts: 20
Joined: Thu May 17, 2007 11:37 pm

Getting length of a field in characters within a structure

Post 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.
Last edited by Prof on Tue Jun 05, 2007 6:47 pm, edited 2 times in total.
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post 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
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
Prof
User
User
Posts: 20
Joined: Thu May 17, 2007 11:37 pm

Post 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.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Prof
User
User
Posts: 20
Joined: Thu May 17, 2007 11:37 pm

Post 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.
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post by Thalius »

Oh misunderstood then ;)

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

Glad it works now..

Thalius
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
Post Reply