Posts: 394
Threads: 58
Joined: Apr 2022
Reputation:
13
I was wondering if anyone has worked with Virtual Arrays? These are arrays which would reside on your hard drive rather than in ram or your computer memory. From what I recently read about them, they go back to an earlier time, circa 1980's, and were part of the Basic language. The worked along the lines of Files in terms that you would open and close them, use Input or Output and a number. So to declare them it would look something like this:
Dim #1, myArray(5000)
Open "myArray" for Input as #1
As I understand it, the virtual array enjoyed a random access feature and its size was only limited to the size of your hard drive or thumb drive.
And before you say it, I know if anyone responds to this enquiry you may be tipping off your age.
Posts: 4,020
Threads: 181
Joined: Apr 2022
Reputation:
225
12-15-2024, 02:58 PM
(This post was last modified: 12-15-2024, 03:01 PM by bplus.)
Yes Random Access is like arrays permanently stored in files. Back in 80's I stored prime numbers in a massive file, might have been first factor of every number don't remember which.
b = b + ...
Posts: 394
Threads: 58
Joined: Apr 2022
Reputation:
13
Ya, I wonder what the difference would have been back then to work with either a File opened for Random access or a Virtual Array. I have multiple files which are constantly receiving updated data. They are pretty large now, can't imagen working with an array at the sizes of those files.
Posts: 2,220
Threads: 226
Joined: Apr 2022
Reputation:
110
I coded the same back in the days where memory was at a premium. 2 + 2 is... wait for it.... 4
Pete
- I'm not as old as the hills, but in all fairness, the hills are better looking.
Posts: 740
Threads: 104
Joined: Apr 2022
Reputation:
14
(12-15-2024, 02:58 PM)bplus Wrote: Yes Random Access is like arrays permanently stored in files. Back in 80's I stored prime numbers in a massive file, might have been first factor of every number don't remember which. Each value would have to be written with the same number of bytes, right?
Can someone who knows how to do random access files post an example?
Posts: 4,020
Threads: 181
Joined: Apr 2022
Reputation:
225
12-16-2024, 03:29 PM
(This post was last modified: 12-16-2024, 03:33 PM by bplus.)
Yes same amount of bytes for each record which could be divided into fields numeric type and/or fixed length strings.
We did setup an RA for Dictionary words and definitions for PhilOfPerth, simple word and definition per record, try search?
Oh I might have quickie example:
Code: (Select All) _Title "UDT to Random Access File Test" ' b+ 2021-12-24
Type Image
As String * 255 Text, FileName
As Long SzX, SzY, PosX, PosY
End Type
Const SW = 1000, SH = 700
ReDim As Image TheItem(1 To 100), TheRecord
TheItem(1).Text = FakeText$(255)
TheItem(1).FileName = FakeText$(255)
Print TheItem(1).Text
Print Len(TheItem(1).Text)
Print TheItem(1).FileName
Print Len(TheItem(1).FileName)
Print "zzz"
Sleep
'make fake data to file
For i = 1 To 100
TheItem(i).Text = FakeText$(255)
TheItem(i).SzX = 100 + Rnd * 20
TheItem(i).SzY = 70 + Rnd * 14
TheItem(i).PosX = Rnd * (SW - TheItem(i).SzX)
TheItem(i).PosY = Rnd * (SH - TheItem(i).SzY)
TheItem(i).FileName = FakeText$(255)
Next
Open "Data Dump.RA" For Random As #1 Len = Len(TheRecord)
For i = 1 To 100
'odious and tedious is this
TheRecord.Text = TheItem(i).Text
TheRecord.SzX = TheItem(i).SzX
TheRecord.SzY = TheItem(i).SzY
TheRecord.PosX = TheItem(i).PosX
TheRecord.PosY = TheItem(i).PosY
TheRecord.FileName = TheItem(i).FileName
Put #1, , TheRecord
Next
Close #1
Print "Data File Ready"
' OK we got data filed! Now can we get it back
Open "Data Dump.RA" For Random As #1 Len = Len(TheRecord)
For i = 1 To 100
Cls
Get #1, i, TheRecord
Print "Record Number:"; i
Print "Text: "; TheRecord.Text
Print "SzX:"; TheRecord.SzX
Print "SzY:"; TheRecord.SzY
Print "PosX:"; TheRecord.PosX
Print "PosY:"; TheRecord.PosY
Print "FileName:"; TheRecord.FileName
Print " zzz..."
Sleep
Next
Close #1
Function FakeText$ (lengthh)
BlankString$ = Space$(lengthh)
fini = Int(Rnd * 255) + 1
For i = 1 To fini
Mid$(BlankString$, i, 1) = Chr$(Rnd * (96 - 32) + 32)
Next
FakeText$ = BlankString$
End Function
b = b + ...
Posts: 394
Threads: 58
Joined: Apr 2022
Reputation:
13
12-16-2024, 04:30 PM
(This post was last modified: 12-16-2024, 04:34 PM by Dimster.)
Hi Madscijr, here's some code which Steve drew up for me a while ago and it helped a lot, hope it's what you could be looking for.
Code: (Select All) _Title "Random Access File Demo"
Type Person_Type 'a type to hold your records
FirstName As String * 20
LastName As String * 20
Phone As _Unsigned _Integer64
End Type
Dim PhoneRecord As Person_Type 'and a variable to reference that type
Open "Phone Database" For Random As #1 Len = Len(PhoneRecord) 'open file at the length of one of those records.
Do
Cls
RecordLimit = LOF(1) \ Len(PhoneRecord) 'the number of records in your file.
Print "Record"; Tab(5); "Last Name"; Tab(30); "First Name"; Tab(55); "Phone Number"
For i = 1 To RecordLimit
Get #1, i, PhoneRecord 'get the whole record
Print i; Tab(5); PhoneRecord.LastName; Tab(30); PhoneRecord.FirstName; Tab(55); PhoneRecord.Phone
Next
Print
Print "What to do: 1)Add a record"
Print " 2)Delete a record"
Print " 3)Edit a record"
Print " 4)QUIT"
Do
a = Val(Input$(1))
Loop Until a > 0 And a < 5
Print a
Print
Select Case a
Case 1 'add a record
RecordLimit = RecordLimit + 1
Print "Adding Record #"; RecordLimit
Input "Enter Last Name:"; PhoneRecord.LastName
Input "Enter First Name:"; PhoneRecord.FirstName
Input "Enter Phone Number:"; PhoneRecord.Phone
Put #1, RecordLimit, PhoneRecord
Case 2 'delete a record
Input "Which record to delete?"; DeleteRecord
For i = DeleteRecord To RecordLimit - 1 'shift all records from deletion point on back one spot
Get #1, i + 1, PhoneRecord
Put #1, i, PhoneRecord
Next
Close #1
a$ = Space$(Len(PhoneRecord) * (RecordLimit - 1)) 'soace for all but the last record
Open "Phone Database" For Binary As #1
Get #1, 1, a$ 'get the whole database (minus the deleted record)
Close #1
Open "Phone Database" For Output As #1: Close 'delete the old database
Open "Phone Database" For Binary As #1 'save the new database to the drive.
Put #1, 1, a$
Close #1
Open "Phone Database" For Random As #1 Len = Len(PhoneRecord) 'open file at the length of one of those records.
Case 3 'edit a record
Input "Which Record to edit?"; editrecord
If editrecord > 0 And editrecord <= RecordLimit Then
Print "Editing Record #"; editrecord
Input "Enter Last Name:"; PhoneRecord.LastName
Input "Enter First Name:"; PhoneRecord.FirstName
Input "Enter Phone Number:"; PhoneRecord.Phone
Put #1, editrecord, PhoneRecord
Else
Print "Invalid record number."
End If
Case 4 'system
End Select
Loop
And oh yes, bplus also gave me a neat addition to this phone record demo where he explained how you could put a Random File within a Random File where I had been working with multiple additional entries like multiple phone numbers. I haven't found that example code but if this is what you are looking for I'll try to find that example as well?
Posts: 740
Threads: 104
Joined: Apr 2022
Reputation:
14
Yesterday, 03:49 AM
(This post was last modified: Yesterday, 03:51 AM by madscijr.)
(12-16-2024, 03:29 PM)bplus Wrote: Yes same amount of bytes for each record which could be divided into fields numeric type and/or fixed length strings.
We did setup an RA for Dictionary words and definitions for PhilOfPerth, simple word and definition per record, try search?
Oh I might have quickie example:
... (12-16-2024, 04:30 PM)Dimster Wrote: Hi Madscijr, here's some code which Steve drew up for me a while ago and it helped a lot, hope it's what you could be looking for.
...
And oh yes, bplus also gave me a neat addition to this phone record demo where he explained how you could put a Random File within a Random File where I had been working with multiple additional entries like multiple phone numbers. I haven't found that example code but if this is what you are looking for I'll try to find that example as well?
Thanks to you both - this could come in handy!
|