FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour "Shortname" for FIELD of DBF
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
"Shortname" for FIELD of DBF
Posted: Fri Jan 13, 2023 10:58 PM
hi,

when "read" XLSx via Excel ActiveX i often have "Header" which are longer than 10 Sign for FIELD in DBF
those "Header" Name can be equal on SUBSTR("Headername",1,10) so i need a "Shortname"

did somebody know a Way to get a unique "Shortname" from String which is longer than 10 Sign :?:
greeting,

Jimmy
Posts: 6984
Joined: Fri Oct 07, 2005 07:07 PM
Re: "Shortname" for FIELD of DBF
Posted: Sat Jan 14, 2023 08:46 AM

Dear Jimmy,

I tried

ChatGPT

:=)

Best regards and have a nice Sunday,

Otto

hi,

when "read" XLSx via Excel ActiveX i often have "Header" which are longer than 10 Sign for FIELD in DBF

those "Header" Name can be equal on SUBSTR("Headername",1,10) so i need a "Shortname"

did somebody know a Way to get a unique "Shortname" from String which is longer than 10 Sign

There are several ways to create a unique "shortname" from a string that is longer than 10 characters:

Use a hash function: You can use a hash function such as SHA-1 or MD5 to generate a unique shortname. This will ensure that even if the original strings are similar, the shortnames will be different.

Use a combination of the first and last characters: You can use the first and last characters of the original string to create a unique shortname. For example, if the original string is "abcdefghijklmnopqrstuvwxyz", the shortname could be "a-z".

Use a counter: You can add a counter to the end of the shortname to make it unique. For example, if the original string is "abcdefghijklmnopqrstuvwxyz", the first shortname could be "abcdefghij", the second could be "abcdefghik" and so on.

Use a combination of letters and numbers: You can use a combination of letters and numbers such as a timestamp to make the shortname unique.

Use a unique id generator library: You can use a library that generates unique ids, such as uuid.

Ultimately, the best approach will depend on your specific use case and requirements.

how to do this with harbour

Here's an example of how you could create a unique "shortname" from a string that is longer than 10 characters using the Harbour programming language:

Use the SUBSTR() function to get the first 10 characters of the original string:

Copy code

shortname := SUBSTR( original_string, 1, 10 )

Use the HASH() function to create a hash of the original string:

Copy code

shortname := HASH( original_string, "md5" )

Use the first and last characters of the original string:

Copy code

shortname := SUBSTR( original_string, 1, 1 ) + "-" + SUBSTR( original_string, LEN( original_string ), 1 )

Use a counter:

Copy code

static counter := 0

shortname := SUBSTR( original_string, 1, 10 ) + "-" + counter

counter += 1

Use a combination of letters and numbers:

Copy code

shortname := SUBSTR( original_string, 1, 8 ) + "_" + DATE()

Use a unique id generator library:

Copy code

shortname := UUID()

You can use any of the above approach to get a unique shortname, depending on your specific use case and requirements.

Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: "Shortname" for FIELD of DBF
Posted: Sat Jan 14, 2023 04:05 PM
Jimmy wrote:hi,

when "read" XLSx via Excel ActiveX i often have "Header" which are longer than 10 Sign for FIELD in DBF
those "Header" Name can be equal on SUBSTR("Headername",1,10) so i need a "Shortname"

did somebody know a Way to get a unique "Shortname" from String which is longer than 10 Sign :?:
At first I simply did something like this : (there was a time I was thinking it only was 8 chars )))
Code (fw): Select all Collapse
function veldnaam()
   Local aTemp:={}  // as a sample data
   AADD(aTemp,"Naam")
   AADD(aTemp,"HOOFDINGVELD1")
   AADD(aTemp,"HOOFDINGVELD2")
   AADD(aTemp,"PRICE")

  n=0
  for i = 1 to len(aTemp)
     cField = aTemp[i]
     if len( cField ) > 7
     n++
     cField := substr( cField, 1, 7 - len ( alltrim( str( n, 0 ) ) ) ) + ;
           padl( alltrim( str( n, 0) ), 2, "0" )
     endif
     aTemp[i] = cField
  next
  msginfo(atostr(aTemp))

return
But now, since I have to read dozens of xls regualar, I once match the fields with the headers in a xbrowse, make the dbf and read it.

also as third...

Read the first line of exel with OLE, changes the headers into the xls and save it. Then use exeltodbf function to generate the dbf

If needed one of these, I have some basic code for it
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: "Shortname" for FIELD of DBF
Posted: Sat Jan 14, 2023 08:47 PM
hi Otto,
Otto wrote:I tried ChatGPT
Answer from ChatGPT might work ... but a Hash is not "readable" for Human

as Marc say to change last 2 Sign (of 10) is enough to make it unique for 99 "Header"
greeting,

Jimmy

Continue the discussion