"Shortname" for FIELD of DBF

"Shortname" for FIELD of DBF

Postby Jimmy » 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
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: "Shortname" for FIELD of DBF

Postby Otto » Sat Jan 14, 2023 8: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.
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: "Shortname" for FIELD of DBF

Postby Marc Venken » Sat Jan 14, 2023 4: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: Select all  Expand view
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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: "Shortname" for FIELD of DBF

Postby Jimmy » Sat Jan 14, 2023 8: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
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 91 guests