Champak Posted July 14, 2023 Posted July 14, 2023 I have a sku system with a prefix of a letter and number then a dash and seven subsequent numbers. These skus are in a 1D array. So let's say I have skus: C2-0000010 C2-0000011 C2-0000012 C1-0000010 C1-0000011 A4-0000012 A4-0000013 I want to enter C1 and it searches and returns all skus with C1. So I tried to do a _arrayfindall with with a partial match, but that won't work because it will return based on just the first letter. Then I saw I can use regexp as the comparison value, but I don't know how to do that and I haven't been able to find any examples. Any help please, thanks.
Andreik Posted July 14, 2023 Posted July 14, 2023 #include <Array.au3> Local $aSKU[7] = ['C2-0000010', 'C2-0000011', 'C2-0000012', 'C1-0000010', 'C1-0000011', 'A4-0000012', 'A4-0000013'] $aResult = Search($aSKU, 'C1') _ArrayDisplay($aResult) Func Search(ByRef $avData, $sSearch) Local $aResult[1], $iCurrent = 0 If Not IsArray($avData) Then Return Null For $Index = 0 To UBound($avData) - 1 If StringInStr($avData[$Index], $sSearch) Then ReDim $aResult[$iCurrent + 1] $aResult[$iCurrent] = $avData[$Index] $iCurrent += 1 EndIf Next Return $aResult EndFunc Something like this?
pixelsearch Posted July 14, 2023 Posted July 14, 2023 (edited) @Champak Concerning the _ArrayFindAll() with regex, this should do it in your case : Local $aResult = _ArrayFindAll($aArray, "^C1.*", 0, 0, 0, 3) ; okEdit: you can even make it shorter, dropping the ".*" part, like this : Local $aResult = _ArrayFindAll($aArray, "^C1", 0, 0, 0, 3) ; better A bad idea would be to search only for "C1" . Though it would work in your specific example, it will bring too many results if a row contained, for example, "A4-0000C12" Local $aResult = _ArrayFindAll($aArray, "C1", 0, 0, 0, 3) ; not recommended Edited July 14, 2023 by pixelsearch "I think you are searching a bug where there is no bug..."
Solution mikell Posted July 14, 2023 Solution Posted July 14, 2023 (edited) regex, what else ? #include <Array.au3> Local $aSKU[8] = ['C2-0000010', 'C2-0000011', 'A4-0000C12', 'C2-0000012', 'C1-0000010', 'C1-0000011', 'A4-0000012', 'A4-0000013'] $search = "C1" $res = StringRegExp(_ArrayToString($aSKU), $search & '-\w+', 3) _ArrayDisplay($res) Edit I didn't read the last comment of pixelsearch Edited July 14, 2023 by mikell
Andreik Posted July 14, 2023 Posted July 14, 2023 (edited) @pixelsearch How could a row contain A4-0000C12 if he said the pattern is a prefix of a letter and number then a dash and seven subsequent numbers? PS: and please don't say it's a hex number Edited July 14, 2023 by Andreik
pixelsearch Posted July 14, 2023 Posted July 14, 2023 (edited) @Andreik Because I always try to bring a solution that fits more than what OP is asking. Tomorrow he may add a post saying : "now I got a problem, because I got rows like 'A4-0000C12' and the solution you provided is not working anymore, what should I do now ?" When you can provide a solution that works in more cases than what OP is asking, just do it. What is crucial is that rows starting with "C1" are found and the regex "^" solves this, the rest isn't important. By the way I didn't comment your way of solving it, with endless Redim statements, which is bad programming, or with the fact that OP needed to understand "_arraysearch with regexp as compare" as stipulated in his post and the title of his thread, which you didn't answer at all. I"ll comment it now as you always got tendentious comments (with anybody) when users don't stick with the solution you provided. I didn't want to write any of this but enough is enough. Please refrain from commenting on my answers, in the same way I'll never comment on yours from now on.@mikell thanks for your "Edit" comment, much appreciated Edited July 14, 2023 by pixelsearch "I think you are searching a bug where there is no bug..."
Andreik Posted July 14, 2023 Posted July 14, 2023 (edited) Don't be mad kid, you have a whole life to prove your programming skills or their lack. I you read carefully he never said it's looking for a regex solution, he said what he tried. Actually what he need it's this: Quote I want to enter C1 and it searches and returns all skus with C1. You can count this as another tedious comment and let me know when I have a high count. Edited July 14, 2023 by Andreik
mikell Posted July 15, 2023 Posted July 15, 2023 9 hours ago, Andreik said: Don't be mad kid, you have a whole life ... etc etc As he's probably older than you, that sounds funny - kinda uppish though ... keep cool old timer, everybody around the place knows you're always rightÂ
Andreik Posted July 15, 2023 Posted July 15, 2023 Probably but you'll never know. It's not about wrong or right, I asked a question and he had a mental breakdown crying that it's enough and commenting my programming skill. This is why I called him a kid. If you have something to complain about, use the report button and don't be a drama queen. We have enough moderators around to keep things under control, we don't need wanna be moderators. If you and pixelsearch can't deal with public forums and with people asking questions you can choose to watch Tom & Jerry or something else.
Champak Posted July 15, 2023 Author Posted July 15, 2023 Thanks all, @Andreik your solution worked perfectly thanks. @pixelsearch thanks for pointing out how that regexp worked, that was not the way I would have gone about it. I'm sure I'll find use for that in the future.
Andreik Posted July 15, 2023 Posted July 15, 2023 You can use this version, since some are concerned about the execution time. #include <Array.au3> Local $aSKU[7] = ['C2-0000010', 'C2-0000011', 'C2-0000012', 'C1-0000010', 'C1-0000011', 'A4-0000012', 'A4-0000013'] $aResult = Search($aSKU, 'C1') _ArrayDisplay($aResult) Func Search(ByRef $avData, $sSearch) Local $sResult If Not IsArray($avData) Then Return Null For $Index = 0 To UBound($avData) - 1 If StringInStr($avData[$Index], $sSearch) Then $sResult &= $avData[$Index] & Chr(0) Next Return StringSplit(StringTrimRight($sResult, 1), Chr(0), 2) EndFunc
Champak Posted July 15, 2023 Author Posted July 15, 2023 That is faster, thanks for keeping on it, BUUUUUT, I have to change the solution to mikell, I missed it the first time around and it is consistently lightning FAAAASTÂ .
Andreik Posted July 15, 2023 Posted July 15, 2023 There is no problem, whatever fits you best. In my tests with an array of 10k elements I couldn't get a difference greater than 10 ms, and if I replace the StringInStr() with StringRegExp() the difference it's like 1 or 2 ms less or high.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now