How do I create a UUID (GUID) in Livecode (or HyperTalk)?

How do I create a UUID (= Universally unique identifier, or GUID = Globally Unique Identifier, Microsoft speak) in Livecode or Hypercard?

The aim of UUIDs is to give practically unique keys to pieces of information without a central coordination.

References

  • Type 4 UUID (Wikipedia)
  • How to create a GUID in JavaScript
  • Is there any difference between GUID and UUID?

  • In LiveCode 6.1 (released today) you can create a uuid using the uuid function. Type 4 random uuid is the default and type 3 and 5 digest based uuids are also implemented.


    If you're on a Unix (like Linux or MacOS), you could use the shell() function to call the uuidgen Terminal command. It should be something like

    put shell("uuidgen") into theUUID
    

    That's a bit heavy-handed (creates a shell, runs a command line application in it, then quits it again), but will work on older LiveCode versions, and isn't that different from what a shell script does.

    In HyperCard, you'd have to use AppleScript, either in an object whose script is set to AppleScript, or using the "do X as AppleScript" command. Not sure if AppleScript can natively build UUIDs, but if it can't, AppleScript can be used to run shell scripts. (The shell() function doesn't exist in HyperCard, it was invented by SuperCard, IIRC).

    In case none of that helps, here's a specification that describes how to create a standard UUID: http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt It's not specific to any programming language.


    以下函数创建一个4型(随机)UUID:

    function getUUID
       local tUUIDpattern
       local tUUID
       local tHexDigits
       put "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx" into tUUIDpattern
       put "1234567890abcdef" into tHexDigits
       repeat for each char tChar in tUUIDpattern
          if tChar = "x" then
             put any char of tHexDigits  after tUUID
          else
             put tChar after tUUID
          end if
       end repeat
       return tUUID
    end getUUID
    
    链接地址: http://www.djcxy.com/p/91446.html

    上一篇: Windows 10在C ++中为UWP生成一个UUID

    下一篇: 如何在Livecode(或HyperTalk)中创建UUID(GUID)?