Kowolsky Posted October 24, 2018 Share Posted October 24, 2018 Hi all, I am trying to figure out what function I would need to accomplish the following scenario. I am not looking for anyone to write any code for me (as I think I may be able to figure it out by myself), but perhaps just to point me in the right direction as what I would need to research and apply to my code. Scenario: 1) I have a text file (Let's call it File A - which gets updated on a daily basis) with 10 rows. Each row has text in it, but tomorrow, the data on each row will be different. This text is comma delimited. Row1: Rocks,20 Row2: Stones,10 Row3: Pebbles,30 And so on until row 10. 2) I have another text file (Let's call it File B - and it acts as a sort of database file). This file will never change. This file is also comma delimited. Row1: Cats,10 Row2: Dogs,20 Row3: Rocks,30 Row4: Pebbles,40 The ultimate goal of this would be so that whatever information is received in File A is then searched in File B and the value after the comma in File B is given. So in the example above, File A has Rocks, Stones and Pebbles.... The result that is returned would be Rocks = 30, Pebbles = 40, but NO result is returned for Stones as as it does not exist in File B. All this is going to do is to track price changes. Would I be looking at using multiple arrays in order to achieve this? I am guessing the that 1st array would be used to sort out the data in File A, and then the 2nd array for the File B. Then use those results to work it out. Thank you, Kowolsky Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted October 24, 2018 Share Posted October 24, 2018 (edited) Hi @Kowolsky Even if you wrote these wonderful words which are rare here 38 minutes ago, Kowolsky said: I am trying to figure out what function I would need to accomplish the following scenario. I am not looking for anyone to write any code for me (as I think I may be able to figure it out by myself), but perhaps just to point me in the right direction as what I would need to research and apply to my code. I'd like to give you a good example to understand the mechanism of what you're trying to do expandcollapse popupGlobal $strFileA = @ScriptDir & "\FileA.txt", _ $strFileB = @ScriptDir & "\FileB.txt", _ $arrFileA_Content, _ $arrFileB_Content ; Using _FileReadToArray, you are able to get the content of a file in an array, and, ; if you have CSV like in your case, you can use the Delimter parameter of _FileReadToArray() function ; at your advantage _FileReadToArray($strFileA, $arrFileA_Content, $FRTA_COUNT, ",") ; Always check for @error(s)! If @error Then ConsoleWrite("Errore while reading the content of the file '" & $strFileA & "' in the array. Error: " & @error & @CRLF) Else ; Do the same with 'FileB.txt' _FileReadToArray($strFileB, $arrFileB_Content, $FRTA_COUNT, ",") If @error Then ConsoleWrite("Errore while reading the content of the file '" & $strFileB & "' in the array. Error: " & @error & @CRLF) Else ; Now you have two (Two-Dimensional) arrays filled with the content of your text files, and you can do whatever you want ; In your case, you want to search a string (which now it's in the first (0th) column of the first array (A), ; which is present in the first (0th) column of the second array (B), and so, you need to loop through these two arrays, ; doing something like this: ConsoleWrite("With double loops: " & @CRLF) For $i = 0 To UBound($arrFileA_Content) - 1 Step 1 For $j = 0 To UBound($arrFileB_Content) - 1 Step 1 If $arrFileA_Content[$i][0] = $arrFileB_Content[$j][0] Then ConsoleWrite($arrFileA_Content[$i][0] & "(A) = " & $arrFileB_Content[$j][0] & "(B) - Number (B) = " & $arrFileB_Content[$j][1] & @CRLF) EndIf Next Next ; Or you could use _ArraySearch as well ConsoleWrite(@CRLF & "With _ArraySearch(): " & @CRLF) For $i = 0 To UBound($arrFileA_Content) - 1 Step 1 $intIndexFound = _ArraySearch($arrFileB_Content, $arrFileA_Content[$i][0]) ; If the function does return -1, it means that the item has not been found in the current index, and so, you have to filter ; this variable to display only the matches. If $intIndexFound <> -1 Then ConsoleWrite($arrFileA_Content[$i][0] & "(A) = " & _ $arrFileB_Content[$intIndexFound][0] & "(B) - Number (B) = " & $arrFileB_Content[$intIndexFound][1] & @CRLF) EndIf Next EndIf EndIf Cheers Edited October 24, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted October 24, 2018 Moderators Share Posted October 24, 2018 (edited) @Kowolsky yes, the easiest way would be to do an array for each file. You could probably do it easier with a regex, here is the easiest pseudo code I can type on my phone Should give you an idea anyway: given text file A: Rocks,20 Stones,10 Pebbles,30 Cats,60 Dogs,80 Children,2 Tables,40 Windows,56 CoWorkers,98 Laptops,33 and text file B: Rocks,35 Stones,20 Pebbles,52 Cats,40 Dogs,70 Windows,16 CoWorkers,78 Laptops,33 Something like this (pseudo): #include <File.au3> Local $aFileA = FileReadToArray(@DesktopDir & "\FileA.txt") Local $aFileB = FileReadToArray(@DesktopDir & "\FileB.txt") For $sLine In $aFileA For $sElement in $aFileB If (StringSplit($sLine, ","))[1] = (StringSplit($sElement, ","))[1] Then ConsoleWrite("Change for " & (StringSplit($sLine, ","))[1] & " = " & ((StringSplit($sLine, ","))[2] - (StringSplit($sElement, ","))[2]) & @CRLF) EndIf Next Next Edited October 24, 2018 by JLogan3o13 Typo "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
Kowolsky Posted October 24, 2018 Author Share Posted October 24, 2018 Thank you @FrancescoDiMuro & @JLogan3o13 for the quick responses. I will take a look at this in more depth this evening. Have a great day! Cheer Link to comment Share on other sites More sharing options...
gruntydatsun Posted October 26, 2018 Share Posted October 26, 2018 here's how i'd do it (error checking left out to make it easy to read) #include <array.au3> #include <file.au3> #include <MsgBoxConstants.au3> Global $arrA, $arrB Global $SearchTerm = "Stones" _FileReadToArray(@ScriptDir & "\FileA.txt",$arrA, $FRTA_NOCOUNT,",") _FileReadToArray(@ScriptDir & "\FileB.txt",$arrB, $FRTA_NOCOUNT,",") msgbox(1,"RESULT","Value of " & $SearchTerm & " is " & GetValue($SearchTerm)) Func GetValue($SearchTerm) $indexB = _ArraySearch($arrB,$SearchTerm) if ($indexB <> -1) Then return $arrB[$indexB][1] $indexA = _ArraySearch($arrA,$SearchTerm) if ($indexA <> -1) Then return $arrA[$indexA][1] EndFunc Link to comment Share on other sites More sharing options...
gruntydatsun Posted October 26, 2018 Share Posted October 26, 2018 or using regular expressions (not my forte so be gentle) Global $FindThis = "Rocks" msgbox(1,"Match on " & $FindThis, GetValue($FindThis)) Func GetValue($SearchTerm) Local $fileA = FileRead(@ScriptDir & "\FileA.txt") Local $fileB = FileRead(@ScriptDir & "\FileB.txt") Local $regex = $SearchTerm & ",(\d.*)" $array = StringRegExp($fileB,$regex,3) if @error = 0 then return $array[0] $array = StringRegExp($fileA,$regex,3) if @error = 0 then return $array[0] EndFunc Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted October 26, 2018 Share Posted October 26, 2018 @gruntydatsun The fact is that the OP didn't ask for a single word search, but: On 24/10/2018 at 2:34 PM, Kowolsky said: The ultimate goal of this would be so that whatever information is received in File A is then searched in File B and the value after the comma in File B is given. So, your copy-pasted scripts won't do what the OP asked for Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
gruntydatsun Posted October 26, 2018 Share Posted October 26, 2018 @FrancescoDiMuro ... mate that is needlessly combative. Is it normal behaviour here these days to attack people who are trying to help? Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted October 26, 2018 Share Posted October 26, 2018 @gruntydatsun I wasn't trying to attack you in any way... But stepping in a thread where a couple of people have already helped the OP asking his question, and not copy-pasting the code, making it discordant from what the OP asked, I don't see so much help in that. Said this, I won't reply at this anymore. Have a good day Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Developers Jos Posted October 26, 2018 Developers Share Posted October 26, 2018 3 hours ago, gruntydatsun said: mate that is needlessly combative. Is it normal behaviour here these days to attack people who are trying to help? Ever considered that it also could be the receiver of the message that is misinterpreting the send message? To me, only facts were listed.... You might not like that, but hey that really is your problem. We are here in a community with many none English folks, so many will simply translate the word without considering the emotions of the receiver as they do not master the Language enough to even know that words could have a different "emotional" meaning. 2 hours ago, FrancescoDiMuro said: Said this, I won't reply at this anymore. Have a good day Same counts for you: When somebody reacts like that you could simply think "fuck it" and ignore that member and keep on doing what you enjoy. That is basically what I do each day around here. Just my 2 cents (from another none English person) Jos FrancescoDiMuro 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted October 26, 2018 Share Posted October 26, 2018 (edited) Hey @Jos As I was saying to @gruntydatsun, it wasn't my intent to offend someone, especially if he was trying to help! What I was saying ( with a calm feeling ), is that since I've been "warned" from other users when I misunderstood some requests from OPs, I wanted to inform him that the OP didn't ask for what he was proposing, but something that was already discussed in the thread. 10 minutes ago, Jos said: Same counts for you: When somebody reacts like that you could simply think "fuck it" and ignore that member and keep on doing what you enjoy. As I wrote here above, I wanted to inform him, so, no hostile behavior... My "I won't reply anymore" is "I don't give a **** of what you'll write from now on", but I want to clarify what I was meaning with my "exclamation". By the way, from a wiser and older person who you are, I can only learn, so, I will keep that in mind. Have a good day you too Jos Edited October 26, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
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