Jump to content

Recommended Posts

Posted

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.

Posted
#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?

Posted (edited)
@Champak Concerning the _ArrayFindAll() with regex, this should do it in your case :

Local $aResult = _ArrayFindAll($aArray, "^C1.*", 0, 0, 0, 3) ; ok

Edit: 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 by pixelsearch

"I think you are searching a bug where there is no bug..."

  • Solution
Posted (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  :P

Edited by mikell
Posted (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? :wacko:

PS: and please don't say it's a hex number :)

Edited by Andreik
Posted (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 by pixelsearch

"I think you are searching a bug where there is no bug..."

Posted (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.  :bonk:

Edited by Andreik
Posted
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  :idiot:

Posted

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. :tv_happy:

Posted

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.

Posted

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
Posted

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 :drool:.

Posted

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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...