$Hsh: Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
 
mNo edit summary
Line 2: Line 2:
<b>Syntax</b>
<b>Syntax</b>
<p>The format for the $HSH function is:</p>
<p>The format for the $HSH function is:</p>
<p class="code">%variable = $HSH('string')
<p class="syntax">%variable = $HSH('string')
</p>
</p>
<p>where:</p>
<p>where:</p>
Line 47: Line 47:
<b></b>*
<b></b>*
   %IDX = $MOD($HSH(%CUST_REC:NAME),500)
   %IDX = $MOD($HSH(%CUST_REC:NAME),500)
   IF %CUST_ARRAY:NAME(%IDX) EQ '' THEN
   IF %CUST_ARRAY:NAME(%IDX) EQ <nowiki>''</nowiki> THEN
       %CUST_ARRAY:NAME(%IDX) = %CUST_REC:NAME
       %CUST_ARRAY:NAME(%IDX) = %CUST_REC:NAME
       %CUST_ARRAY:RATE(%IDX) = %CUST_REC:RATE
       %CUST_ARRAY:RATE(%IDX) = %CUST_REC:RATE
Line 57: Line 57:
       FOR %RETRY FROM 1 TO 5
       FOR %RETRY FROM 1 TO 5
         %IDX = $MOD($HSH(%IDX),500)
         %IDX = $MOD($HSH(%IDX),500)
IF %CUST_ARRAY:NAME(%IDX) EQ '' THEN
IF %CUST_ARRAY:NAME(%IDX) EQ <nowiki>''</nowiki> THEN
             %CUST_ARRAY:NAME(%IDX) = %CUST_REC:NAME
             %CUST_ARRAY:NAME(%IDX) = %CUST_REC:NAME
             %CUST_ARRAY:RATE(%IDX) = %CUST_REC:RATE
             %CUST_ARRAY:RATE(%IDX) = %CUST_REC:RATE

Revision as of 21:02, 5 June 2013

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 User Language 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