Jump to content

# Intervals in a 2D array

## Recommended Posts

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 by Emanoel
##### Share on other sites

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```

##### Share on other sites

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

##### Share on other sites

@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 by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

##### Share on other sites
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🙌🏼

##### Share on other sites

Show the code you used that produce the error...

Not much of a signature but working on it...

Spoiler

##### Share on other sites
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 !```

##### Share on other sites
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 by Emanoel
##### Share on other sites
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:

1. Remove the blank lines from your source, so you don't have those in the array;
2. 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:

##### Share on other sites
```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")```

##### Share on other sites

@pixelsearch Your code worked well and did exact the same thing I want. Thank you so much 🙌

Edited by Emanoel
##### Share on other sites

@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 by pixelsearch
##### Share on other sites

@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.

##### Share on other sites
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, ...) ?

##### Share on other sites
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...

##### Share on other sites
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.

##### Share on other sites
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.

Code hard, but don’t hard code...

## 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.

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...