Emanoel Posted January 29, 2021 Share Posted January 29, 2021 (edited) Hi, the problem I have seems simple, but I can't do it properly. There is a unstable 2D array where each row specifies an interval (like the image). I want a variable such as i to start from 0, if it's inside these intervals, a random value should be written in the file, and if it's outside the range (like using else), the value 0 should be written. Also, when the array ends, program ends too. I tried this code: $array[500][2] = [[390, 480], [809, 870], [870, 959], [959, 1139], ...] $i = 0 $max = 60 For $a In $array If ($i >= $a[0]) And ($i <= $a[1]) Then FileWriteLine ($file, $i & @TAB & Random (0, $max)) Else FileWriteLine ($file, $i & @TAB & 0) EndIf Next ;??? where to add $i += 1 I want txt file to be like: 0 0 1 0 ... 390 34.2341.. 391 55.3457.. ... 480 11.7577.. 481 0 482 0 ... Edited January 29, 2021 by Emanoel Link to comment Share on other sites More sharing options...
Subz Posted January 29, 2021 Share Posted January 29, 2021 Maybe something like: Global $g_sFilePath = @ScriptDir & "\FileName.txt" Global $g_a2DArray[][] = [[2, 5],[390, 480], [809, 870], [870, 959], [959, 1139]] Global $g_iMax = 60 For $i = 0 To UBound($g_a2DArray) - 1 If ($i >= $g_a2DArray[$i][0]) And ($i <= $g_a2DArray[$i][1]) Then FileWriteLine ($g_sFilePath, $i & @TAB & Random (0, $g_iMax)) Else FileWriteLine ($g_sFilePath, $i & @TAB & 0) EndIf Next FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
water Posted January 29, 2021 Share Posted January 29, 2021 I'm not sure I fully understand what you try to achive, but here is my first try: Global $aIntervals = [[390, 480], [809, 870], [870, 959], [959, 1139]] Global $iValue = 460 Global $bFound = False For $i = 0 To UBound($aIntervals) - 1 If $iValue >= $aIntervals[$i][0] And $iValue <= $aIntervals[$i][1] Then ; $iValue found in the current interval ConsoleWrite("Value " & $iValue & " found in interval " & $aIntervals[$i][0] & "-" & $aIntervals[$i][1] & @CRLF) $bFound = True ExitLoop ; Exit when the first interval has been found Else ; $iValue not found in the current interval EndIf Next If $bFound = False Then ConsoleWrite($iValue & " not found in any of the intervals!" & @CRLF) My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted January 29, 2021 Share Posted January 29, 2021 (edited) @Emanoel That's not how array work. In this case, you have to specify the element from the second dimension you want to retrieve, or you are (currently) getting anything from the script above. This should do what you are looking for: Test() Func Test() Local $strFileName = "SomeFileName.txt", _ $arrRanges[4][2] = [[390, 480], [809, 870], [870, 959], [959, 1139]], _ $intMaxValue = 60 For $i = 0 To UBound($arrRanges) - 1 Step 1 FileWriteLine($strFileName, ((($i >= $arrRanges[$i][0]) And ($i <= $arrRanges[$i][1])) ? $i & @TAB & Random(0, $intMaxValue) : $i & @TAB & 0)) Next EndFunc P.S.: since while I was writing the script, @Subz has replied, I had to make the things a little bit funnier Edited January 29, 2021 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...
Emanoel Posted January 29, 2021 Author Share Posted January 29, 2021 6 minutes ago, Subz said: Maybe something like: Global $g_sFilePath = @ScriptDir & "\FileName.txt" Global $g_a2DArray[][] = [[2, 5],[390, 480], [809, 870], [870, 959], [959, 1139]] Global $g_iMax = 60 For $i = 0 To UBound($g_a2DArray) - 1 If ($i >= $g_a2DArray[$i][0]) And ($i <= $g_a2DArray[$i][1]) Then FileWriteLine ($g_sFilePath, $i & @TAB & Random (0, $g_iMax)) Else FileWriteLine ($g_sFilePath, $i & @TAB & 0) EndIf Next The problem is $i is not equal to the index of array. Therefore, after a while program gives the error of being out of the array index range. sorry for my english🙌🏼 Link to comment Share on other sites More sharing options...
Nine Posted January 29, 2021 Share Posted January 29, 2021 Show the code you used that produce the error... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
pixelsearch Posted January 29, 2021 Share Posted January 29, 2021 1 hour ago, Emanoel said: ;??? where to add $i += 1 If I understood correctly, you need a loop like this : Global $g_a2DArray[][] = [[2, 5],[390, 480], [809, 870], [870, 959], [959, 1139]] For $i = 0 To $g_a2DArray[UBound($g_a2DArray) - 1][1] ; 0 To 1139 ; it's gonna be a very long text file with hundred of lines ! Link to comment Share on other sites More sharing options...
Emanoel Posted January 29, 2021 Author Share Posted January 29, 2021 (edited) 1 hour ago, Nine said: Show the code you used that produce the error... I checked @Subz code, the problem is that the program continues until last index of array, but I want $i to continue longer than this value till it reaches the last value of the array. I did the same thing of all these codes in this topic, even wrote "For $i = 0 To $array [UBound ($array) - 1] [1]" but $i goes more than array index and shows the error "Array variable has incorrect number of subscripts or subscript dimension range exceeded." which is normal. I even tried another "For j = 0 To UBound ($array) - 1" in "For $i = 0 To $array [UBound ($array) - 1] [1]" but it did not work because I didn't know where to write If-Else to write. For $i = 0 To $array[UBound ($array)-1][1] For $j = 0 To UBound ($array)-1 If ($i >= $array [$j][0]) And ($i <= $array[$j][1]) Then FileWriteLine ($file, $i & @TAB & Random (0, $max)) Else FileWriteLine ($file, $i & @TAB & 0) EndIf Next Next Edited January 29, 2021 by Emanoel Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted January 29, 2021 Share Posted January 29, 2021 1 minute ago, Emanoel said: the problem is that the program continues until last index of array, but I want $i to continue longer than this value till it reaches the last value of the array There are two ways to handle this: Remove the blank lines from your source, so you don't have those in the array; Put an If which verifies that you are dealing with two numbers instead of blank values. 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...
pixelsearch Posted January 29, 2021 Share Posted January 29, 2021 Global $g_sFilePath = @ScriptDir & "\FileName.txt" Global $g_a2DArray[][] = [[2, 5],[390, 480], [809, 870], [870, 959], [959, 1139]] Global $g_iMax = 60, $iElement = 0 ; ConsoleWrite($g_a2DArray[UBound($g_a2DArray) - 1][1] & @lf) For $i = 0 To $g_a2DArray[UBound($g_a2DArray) - 1][1] ; 0 To 1139 Select Case $i < $g_a2DArray[$iElement][0] FileWriteLine ($g_sFilePath, $i & @TAB & 0) Case $i <= $g_a2DArray[$iElement][1] FileWriteLine ($g_sFilePath, $i & @TAB & Random (0, $g_iMax)) Case Else $iElement += 1 $i -= 1 EndSelect Next MsgBox(0, "", "Done") Emanoel 1 Link to comment Share on other sites More sharing options...
Emanoel Posted January 29, 2021 Author Share Posted January 29, 2021 (edited) @pixelsearch Your code worked well and did exact the same thing I want. Thank you so much 🙌 Edited January 29, 2021 by Emanoel Link to comment Share on other sites More sharing options...
pixelsearch Posted January 29, 2021 Share Posted January 29, 2021 (edited) @Emanoel you're welcome, I'm trying now to understand why it works. edit: please have a look at FileWriteLine to use a file handle instead of a file name, it will write much quicker, especially your array got 500 rows, which means thousands of lines to write (is it related to your srt file ?) Edited January 29, 2021 by pixelsearch Link to comment Share on other sites More sharing options...
Emanoel Posted January 29, 2021 Author Share Posted January 29, 2021 @pixelsearch $i is the value of keyframes and the number in front of it indicates the value of a motion in the aftereffects. I wrote this script to make keyframes in some intervals automatically. Link to comment Share on other sites More sharing options...
Emanoel Posted January 29, 2021 Author Share Posted January 29, 2021 1 hour ago, pixelsearch said: @Emanoel you're welcome, I'm trying now to understand why it works. edit: please have a look at FileWriteLine to use a file handle instead of a file name, it will write much quicker, especially your array got 500 rows, which means thousands of lines to write (is it related to your srt file ?) I changed some lines in my code. Now I create the array as much as the intervals exist. And yes, it is related to srt file, I export time from srt and make keyframes. And by file handle, Do you mean I write FileWriteLine (@ScriptDir & "\FileName.txt", ...) instead of ($file, ...) ? Link to comment Share on other sites More sharing options...
JockoDundee Posted January 29, 2021 Share Posted January 29, 2021 1 hour ago, Emanoel said: And by file handle, Do you mean I write No, he means something like add, Local $Handle = FileOpen($File, $FO_APPEND) at the top, to execute once before the loop. Then, instead of $File, use $Handle, like so: FileWriteLine($Handle, $Data) whenever you write data. Then you should call FileClose($Handle) at the end. Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Emanoel Posted January 29, 2021 Author Share Posted January 29, 2021 1 hour ago, JockoDundee said: No, he means something like add, Local $Handle = FileOpen($File, $FO_APPEND) at the top, to execute once before the loop. Then, instead of $File, use $Handle, like so: FileWriteLine($Handle, $Data) whenever you write data. Then you should call FileClose($Handle) at the end. Ofcourse I used FileOpen and FileClose before and after the loop, but I didn't write "$FO_APPEND". I don't know is it helpful to have faster file writing or not. Perhaps there is a way like using GPU to speed up file writing. Link to comment Share on other sites More sharing options...
JockoDundee Posted January 30, 2021 Share Posted January 30, 2021 10 hours ago, Emanoel said: Ofcourse I used FileOpen and FileClose before and after the loop Ok, but FileClose uses file handles, yet you seem not to be familiar with them: 13 hours ago, Emanoel said: And by file handle, Do you mean I write... 13 hours ago, Emanoel said: FileWriteLine (@ScriptDir & "\FileName.txt", ...) 10 hours ago, Emanoel said: Perhaps there is a way like using GPU to speed up file writing. I would have to doubt it, GPU’s have strength in numbers having hundreds of cores, but the cores themselves are weak compared to a CPU core. They’re good for parallel processing. But writing to a file on a single device is not easily amendable to parallelism; the contention and race conditions would be tricky. Plus, your data is already at the CPU or DMA, so you would have to get it to the GPU just to start it. Emanoel 1 Code hard, but don’t hard code... 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