$Hsh

From m204wiki
(Redirected from $HSH)
Jump to navigation Jump to search

The $Hsh function lets you convert a string into a hash value, which is a distinct numeric representation of a given string value. You can use $Hsh to build a memory-resident hash table to be used to join on keys without sorting and merging.

Syntax

The format for the $Hsh function is:

%variable = $Hsh('string')

Where:

  • %variable is a %variable within a SOUL procedure
  • string is an alphanumeric string or a string referenced by a VALUE IN statement.

Example

This example is continued on the next page:

BEGIN * * INPUT CUSTOMER CONTROL DATA IMAGE: * IMAGE CUST_REC NAME IS STRING LEN 20 RATE IS STRING LEN 8 DISCOUNT IS STRING LEN 8 END IMAGE * * MEMORY-RESIDENT HASH TABLE ARRAY: * IMAGE CUST_ARRAY ARRAY OCCURS 500 NAME IS STRING LEN 20 RATE IS FLOAT DISCOUNT IS FLOAT END ARRAY END IMAGE * PREPARE IMAGE CUST_REC PREPARE IMAGE CUST_ARRAY * * OPEN INPUT DATASET * OPEN DATASET CUSTINFO FOR INPUT * READ IMAGE CUST_REC FROM CUSTINFO REPEAT WHILE $STATUS = 0 * * INDEX INTO HASH TABLE IS REMAINDER OF HASH OF KEY / SIZE OF * TABLE * %IDX = $MOD($HSH(%CUST_REC:NAME),500) IF %CUST_ARRAY:NAME(%IDX) EQ '' THEN %CUST_ARRAY:NAME(%IDX) = %CUST_REC:NAME %CUST_ARRAY:RATE(%IDX) = %CUST_REC:RATE %CUST_ARRAY:DISCOUNT(%IDX) = %CUST_REC:DISCOUNT ELSE * * HAVE A HASH CONFLICT, RETRY A FINITE NUMBER OF TIMES * FOR %RETRY FROM 1 TO 5 %IDX = $MOD($HSH(%IDX),500) IF %CUST_ARRAY:NAME(%IDX) EQ '' THEN %CUST_ARRAY:NAME(%IDX) = %CUST_REC:NAME %CUST_ARRAY:RATE(%IDX) = %CUST_REC:RATE %CUST_ARRAY:DISCOUNT(%IDX) = %CUST_REC:DISCOUNT JUMP TO READNEXT END IF END FOR PRINT 'MAKE THE HASH TABLE LARGER TO AVOID CONFLICTS' STOP END IF READNEXT: READ IMAGE CUST_REC FROM CUSTINFO END REPEAT * * NOW FIND ORDER RECORDS, NO NEED TO SORT * FDORD: IN ORDERS FIND ALL RECORDS WHERE - ORD DATE IS NUM IN RANGE FROM 89120 TO 89150 STATUS = 'SHIPPED' END FIND FORORD: FOR EACH RECORD IN FDORD %IDX = $MOD($HSH(CUST NAME)) IF %CUST_ARRAY:NAME(%IDX) EQ CUST NAME THEN CALL PRINT_INVOICE ELSE * * HAVE A HASH CONFLICT, RETRY A FINITE NUMBER OF TIMES * FOR %RETRY FROM 1 TO 5 %IDX = $MOD($HSH(%IDX),500) IF %CUST_ARRAY:NAME(%IDX) EQ CUST NAME THEN CALL PRINT_INVOICE JUMP TO NEXTREC END IF END FOR PRINT 'CUSTOMER NAME NOT FOUND =' AND CUST NAME NEXTREC: END FOR END