Hola mis amigos.... :D
I need to access a hash file from within a BASIC program. The key of the hash file is composed by several fields.
What is the BASIC sentence to write in the program, to read a record given all the fields of the key ?
Thanks in advance.
Accessing a hash file with a key composed by several fields
Moderators: chulett, rschirm, roy
Accessing a hash file with a key composed by several fields
Guillermo P. Barsky
Buenos Aires - Argentina
Buenos Aires - Argentina
OPENPATH will open an externally pathed hash file.
If you know the primary keys, construct a variable of all of the fields concatenated with an @TM character between each field. Ex:
KEY=field1:@TM:field2:@TM:field3
The ROW variable will be a dynamic array of the data found at that KEY.
Code: Select all
OPENPATH fully_qualified_filename TO file_handle Else
* put error in opening logic here
End
KEY=field1:@TM:field2:@TM:field3
Code: Select all
READ ROW FROM file_handle, KEY Then
* put found logic here
End Else
* put not found logic here
End
The ROW variable will be a dynamic array of the data found at that KEY.
Kenneth Bland
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
Hi,
Please find my code below.
Deffun DSRMessage(A1, A2, A3) Calling "*DataStage*DSR_MESSAGE"
HashTable = Arg4:Arg1
HashKey = Arg2:@TM:Arg5
ColumnPosition = Arg3
PositionReturn = 0
* Determine if we are returning one column or entire row.
If Num(ColumnPosition) then
ColumnPosition = Int(ColumnPosition)
If ColumnPosition > 0 and ColumnPosition < 99999 Then
PositionReturn = 1
End
End
* Attempt to find the table name in our cache.
Locate HashTable in FilesOpened Setting POS Then
Read Rec From FileHandles(POS), HashKey Then
If PositionReturn Then Ans = Rec<ColumnPosition> Else Ans = Rec
End Else
Ans = DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" )
End
End Else
* Table is not in cache of opened tables, so open it.
OpenPath HashTable To FileHandles(POS) Then
FilesOpened<-1> = HashTable
Read Rec From FileHandles(POS), HashKey Else
Rec = DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" )
End
If PositionReturn And Rec <> DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" ) Then
Ans = Rec<ColumnPosition>
End Else
Ans = Rec
End
End Else
Rec = DSRMessage("DSTAGE_TRX_I_0011" , "**TABLE NOT FOUND**", "" )
Ans = Rec
End
End
regards,
Phani
Please find my code below.
Deffun DSRMessage(A1, A2, A3) Calling "*DataStage*DSR_MESSAGE"
HashTable = Arg4:Arg1
HashKey = Arg2:@TM:Arg5
ColumnPosition = Arg3
PositionReturn = 0
* Determine if we are returning one column or entire row.
If Num(ColumnPosition) then
ColumnPosition = Int(ColumnPosition)
If ColumnPosition > 0 and ColumnPosition < 99999 Then
PositionReturn = 1
End
End
* Attempt to find the table name in our cache.
Locate HashTable in FilesOpened Setting POS Then
Read Rec From FileHandles(POS), HashKey Then
If PositionReturn Then Ans = Rec<ColumnPosition> Else Ans = Rec
End Else
Ans = DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" )
End
End Else
* Table is not in cache of opened tables, so open it.
OpenPath HashTable To FileHandles(POS) Then
FilesOpened<-1> = HashTable
Read Rec From FileHandles(POS), HashKey Else
Rec = DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" )
End
If PositionReturn And Rec <> DSRMessage("DSTAGE_TRX_I_0010" , "**RECORD NOT FOUND**", "" ) Then
Ans = Rec<ColumnPosition>
End Else
Ans = Rec
End
End Else
Rec = DSRMessage("DSTAGE_TRX_I_0011" , "**TABLE NOT FOUND**", "" )
Ans = Rec
End
End
regards,
Phani
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
The code's OK (but, next time, format it and surround it with CODE tags).
My guess is that the arguments and the key values actually in the hashed file do not coincide - maybe there are trailing spaces in the latter.
Make sure that the number of trailing spaces, leading zeroes, etc., is identical. Best practice it to TRIM when loading the hashed file, and to TRIM when preparing the key values for a reference lookup.
When testing the routine, you might contemplate logging informational messages to indicate:
(a) which "record not found" message was issued (there are two paths)
(b) the argument values (use the Quote() function to see exactly
(c) the key value that was used (use Convert(@TM,"|", HashKey))
My guess is that the arguments and the key values actually in the hashed file do not coincide - maybe there are trailing spaces in the latter.
Make sure that the number of trailing spaces, leading zeroes, etc., is identical. Best practice it to TRIM when loading the hashed file, and to TRIM when preparing the key values for a reference lookup.
When testing the routine, you might contemplate logging informational messages to indicate:
(a) which "record not found" message was issued (there are two paths)
(b) the argument values (use the Quote() function to see exactly
(c) the key value that was used (use Convert(@TM,"|", HashKey))
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.