PDA

View Full Version : Pesquisa Dentro da Struct



watanabe
27-Jan-2021, 08:29 AM
bom dia.
Existe alguma forma de pesquisar dentro de uma Struct com Array ?

http://image.prntscr.com/image/FvsA3oodTNSMWsOo8C2-XQ.png

Dentro dessa Struct tenho a matriz value queria pesquisar um id é possível no Dataflex

Mike Peat
27-Jan-2021, 09:15 AM
Eduardo - you can just use SearchArray:



Use Windows.pkg

Struct stTest
Integer[] aiIDs
End_Struct

Function SearchForID String iID Returns Integer
stTest tTest
Integer iRes

Move 1 to tTest.aiIDs[0]
Move 5 to tTest.aiIDs[1]
Move 9 to tTest.aiIDs[2]
Move 22 to tTest.aiIDs[3]
Move 17 to tTest.aiIDs[4]
Move 8 to tTest.aiIDs[5]
Move 45 to tTest.aiIDs[6]

Move (SearchArray(iID, tTest.aiIDs)) to iRes

Function_Return iRes
End_Function

Global_Variable Integer giIndex giID

Input "Enter ID to find" giID
Showln

Get SearchForID giID to giIndex

Show "ID " (String(giID))
If (giIndex = -1) ;
Showln " not found in arrray"
Else ;
Showln " found at position " (String(giIndex))
Inkey WindowIndex


Assuming I understood what it was you wanted to do! ;)

Mike

watanabe
27-Jan-2021, 01:10 PM
Mike, thanks for the feedback, I tried to use it but it was wrong at the moment I'm using "for" ...



Struct st_MsGetMember_team_Value
{Name="@odata.type"}
String odatatype
String id
String deletedDateTime
String classification
DateTime createdDateTime
String[] creationOptions
String description
String displayName
String expirationDateTime
String[] groupTypes
String isAssignableToRole
String mail
Boolean mailEnabled
String mailNickname
String membershipRule
String membershipRuleProcessingState
String onPremisesDomainName
String onPremisesLastSyncDateTime
String onPremisesNetBiosName
String onPremisesSamAccountName
String onPremisesSecurityIdentifier
String onPremisesSyncEnabled
String preferredDataLocation
String preferredLanguage
String[] proxyAddresses
DateTime renewedDateTime
String[] resourceBehaviorOptions
String[] resourceProvisioningOptions
Boolean securityEnabled
String securityIdentifier
String theme
String visibility
String extension_fe2174665583431c953114ff7268b7b3_Educati on_ObjectType
String[] onPremisesProvisioningErrors
End_Struct


//json return
Struct st_Msuserteam_member
{Name="@odata.context"}
String odatacontext
st_MsGetMember_team_Value[] value
End_Struct


st_MsGetMember_team_Value stPesquisa
Integer iPosicao

// Does Not work value to be searched for in the id field
Move (trim('xxxxxxx-xxxx')) to stPesquisa.id


//in the st_user_times structure, the value field, which is an array, has 2 records .... and the id field that I place to search exists in it ...
Move (SearchArray(stPesquisa,stUser_Times.value)) to iPosicao

// result iPosicao always comes -1 and the item to be located is in the arrary
If (iPosicao<>-1) Begin
Send ShowInfoBox (stUser_Times.value[iPosicao].displayName)
End
Else Begin
Send ShowInfoBox (" NOT FOUND ")
End

Mike Peat
27-Jan-2021, 02:30 PM
Edwardo

OK, try:



Use Windows.pkg

Struct st_MsGetMember_team_Value
{Name="@odata.type"}
String odatatype
String id
String deletedDateTime
String classification
DateTime createdDateTime
String[] creationOptions
String description
String displayName
String expirationDateTime
String[] groupTypes
String isAssignableToRole
String mail
Boolean mailEnabled
String mailNickname
String membershipRule
String membershipRuleProcessingState
String onPremisesDomainName
String onPremisesLastSyncDateTime
String onPremisesNetBiosName
String onPremisesSamAccountName
String onPremisesSecurityIdentifier
String onPremisesSyncEnabled
String preferredDataLocation
String preferredLanguage
String[] proxyAddresses
DateTime renewedDateTime
String[] resourceBehaviorOptions
String[] resourceProvisioningOptions
Boolean securityEnabled
String securityIdentifier
String theme
String visibility
String extension_fe2174665583431c953114ff7268b7b3_Educati on_ObjectType
String[] onPremisesProvisioningErrors
End_Struct

//json return
Struct st_Msuserteam_member
{Name="@odata.context"}
String odatacontext
st_MsGetMember_team_Value[] value
End_Struct

Object oTester is a cObject // Only required as this isn't in an object, otherwise put the function in your object and use "Self" in the SearchArray call

Function IDTest st_MsGetMember_team_Value aTestVal st_MsGetMember_team_Value aArrayVal Returns Integer
If (aTestVal.id = aArrayVal.id) ;
Function_Return (EQ)
End_Function

End_Object

st_Msuserteam_member stMember
st_MsGetMember_team_Value stTest
// st_MsGetMember_team_Value stPesquisa
Integer iPosicao

// Dummy data for test
Move "aaaaaaa-bbbb" to stMember.value[0].id
Move "Tim" to stMember.value[0].displayName
Move "xxxxxxx-xxxx" to stMember.value[1].id
Move "Bob" to stMember.value[1].displayName
Move "foo-bar-baz" to stMember.value[2].id
Move "Jim" to stMember.value[2].displayName

// Does Not work value to be searched for in the id field
Move (trim('xxxxxxx-xxxx')) to stTest.id

//in the st_user_times structure, the value field, which is an array, has 2 records .... and the id field that I place to search exists in it ...
Move (SearchArray(stTest, stMember.value, oTester, (RefFunc(IDTest)))) to iPosicao

// result iPosicao always comes -1 and the item to be located is in the arrary
If (iPosicao <> -1) Begin
Send Info_Box (stMember.value[iPosicao].displayName) // value[iPosicao].displayName)
End
Else Begin
Send Info_Box (" NOT FOUND ")
End


PS - the problem is that "id" is not the first member of the struct, which is why you need the custom comparison function. See here (https://docs.dataaccess.com/dataflexhelp/#t=mergedProjects%2FLanguageReference%2FSearchArra y.htm&rhsearch=SearchArray&rhhlterm=SearchArray&rhsyns=%20).

Mike Peat
28-Jan-2021, 03:09 AM
Note: you can avoid the need for the custom comparison function (and the object to contain it, in that example) if you reorganise the Struct so that "id" is the first element, which is something you can do with JSON stuff (which I don't think you can with XML web services, which expect the ordering of elements to match).

If you do that - i.e. changing the struct to:


Struct st_MsGetMember_team_Value
String id
{Name="@odata.type"}
String odatatype
String deletedDateTime
String classification
... etc.


Then just:


Move (SearchArray(stTest, stMember.value)) to iPosicao


Will work, because as it says in the Help:


You can use the simple search style for arrays of any simple data type (https://docs.dataaccess.com/dataflexhelp/mergedProjects/LanguageGuide/Fundamental_Types.htm) and arrays of structs where the first struct member is a simple data type.


Mike