Link to home
Start Free TrialLog in
Avatar of wadeg
wadeg

asked on

Save an array's values

I have created an multi dimendsional array!

What I want to do is save all the values in this array a restore them in the onload event. Is there any way to do this without writing all the values to a text file. then opening and parsing the text file in to on load event?

I am looking for a simple and fast way to do this without creating a huge text file.

this array will be very large!

dim myarray(40,50,25,5)
Avatar of viktornet
viktornet
Flag of United States of America image

you can use

write #1, whatever
and
get#1, whatever

and whatever would be your array..

(not sure about the function's name, but it's something similar)

-vik-
Avatar of wadeg
wadeg

ASKER

I think I found what I am after...

savesetting
getsetting

I tested it in a loop and got it to work so I think I will do it that way.

this also enables me to just change and retrieve one record at a time.

If any one has a good reason why I should not use this method please let me know. or even tell me yes it is a good idea and why fo an easy 50 points!

I am sorry viktornet's but I was looking for a method that I do NOT have to write to a text file!
how do you wanna save your array if you dont wanna write to a file?!?! I don't see your point there... i'm quite interested in saving data without writing to a file... please teach me...
Avatar of wadeg

ASKER

OK what I did still saves it but does not create a text file that I will have to open and parse to use

As I said I have a large multi dimensional array!

dim myarray(35,50,25,5)

this will hold 238,680 different values. I did not want to have to parse such a huge file each time my program loaded.

But I found the saveSetting And GetSetting commands

these are used most often to store values in a program so you can just pick up were you left off the next time you load the program.

the format is this

SaveSetting(AppName As String, Section As String, Key As String, Setting As String)

and

GetSetting(AppName As String, Section As String, Key As String, Default Value)

AppName is the name of the application or project
Section I just used the "UR" and it creates it for you
Key.. this is were I used my arrays index numbers with a | in between them

So myarray(11,22,15,1) I stored in the key "11|22|15|1" so when I use a loop to store an get my values from my array I can compare them to the old values with the getsetting statment

Here is my test code

First I wrote something simmilar that used savesetting to write the value tHE i used this to retieve it and compare it to the value that was stored there

keyp = tl(1) & "|" & tl(2)   my 1st and 2nd demensions
For x = 0 To 25
    For q = 0 To 4
       
    test = GetSetting("MY PROGRAM", "UR", keyp & "|" & x & "|" & q)
        If myarray(x, q) = test Then MsgBox (myarray(x, q))
       
    Next q
Next x

and it worked great and very fast!
Avatar of wadeg

ASKER

OK what I did still saves it but does not create a text file that I will have to open and parse to use

As I said I have a large multi dimensional array!

dim myarray(35,50,25,5)

this will hold 238,680 different values. I did not want to have to parse such a huge file each time my program loaded.

But I found the saveSetting And GetSetting commands

these are used most often to store values in a program so you can just pick up were you left off the next time you load the program.

the format is this

SaveSetting(AppName As String, Section As String, Key As String, Setting As String)

and

GetSetting(AppName As String, Section As String, Key As String, Default Value)

AppName is the name of the application or project
Section I just used the "UR" and it creates it for you
Key.. this is were I used my arrays index numbers with a | in between them

So myarray(11,22,15,1) I stored in the key "11|22|15|1" so when I use a loop to store an get my values from my array I can compare them to the old values with the getsetting statment

Here is my test code

First I wrote something simmilar that used savesetting to write the value tHE i used this to retieve it and compare it to the value that was stored there

keyp = tl(1) & "|" & tl(2)   my 1st and 2nd demensions
For x = 0 To 25
    For q = 0 To 4
       
    test = GetSetting("MY PROGRAM", "UR", keyp & "|" & x & "|" & q)
        If myarray(x, q) = test Then MsgBox (myarray(x, q))
       
    Next q
Next x

and it worked great and very fast!
Avatar of wadeg

ASKER

The reason I want to do this is my program measures change over time.

I need to be able to compare a new value to the value that it used to be! without creating a HUGE text file to parse!

this way I only store and get the specific set of values that I want to compare at any given time!
Avatar of wadeg

ASKER

The reason I want to do this is my program measures change over time.

I need to be able to compare a new value to the value that it used to be! without creating a HUGE text file to parse!

this way I only store and get the specific set of values that I want to compare at any given time!
The SaveSetting function writes these values to the registry of your PC. GetSetting will retrieve these values. That's why there was no file created. Still I don't like using this method, because if you're changing your PC, you have to copy the registry, rather than a file.

kzzm44
uhum, sorry for saying but the user won't like it that you store such a huge amaount of variables in the registry, it will only make his system less stable and slower...

it is east though :) (typically MS; just throw it in the registry)

do you want these variables when the program is already terminated, otherwise how about using modules; public declare your variable..
I think the answer you rejected was exactly what you need!
It SAVES the whole array in a file with just one line of code and READS the whole file into array with one line of code. So you don't have to do any parsing and you don't have to write any loops. I didn't try it myself but I think it has to work even faster then your loops.
Agreed. VictorNet gave the correct answer. You can load the file when needed and you can even choose to only load the parts of the array you need. The file you'd create is less than 1 Meg in size and that is not exacly huge in Windoze terms...
Filling the registry however with such amound of information can have unexpected results. Some systems protest when the registry sizes over a certain size, and an OS like NT will flatly refuse the save if you have no rights to alter the registry.

Wow! They have bugs at Experts! :)
Errr... Why is mirrorinternetzahavnet's name above my entry?
And why my name above the one comment I did not make...

PhiRo
' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 4/05/99
' * Time             : 15:43
' **********************************************************************
' * Comments         : Save an array to a file
' *
' *
' **********************************************************************

Option Explicit
Private maArray() As String

Dim iFileNum   As Integer
Dim sOutput    As String

'Get a free file handle
iFileNum = FreeFile
Open App.Path & "\MyArray.txt" For Output As iFileNum

'Here we make use of this handy new
'method Join.
sOutput = Join(maArray, ";")

'Now we take the string returned to us
'by the Join method and write it to the file
Print #iFileNum, sOutput

Close iFileNum


' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 4/05/99
' * Time             : 15:43
' **********************************************************************
' * Comments         : Read an array from a file
' *
' *
' **********************************************************************

Dim iFileNum As Integer
Dim sInput   As String
Dim iCounter As Integer

'Get a free file handle
iFileNum = FreeFile
Open App.Path & "\MyArray.txt" For Input As iFileNum

'We use the Line Input to read in an
'entire line into our string
Line Input #iFileNum, sInput

'This new Split function will take a string
'split it up, and return an array!
maArray = Split(sInput, ";")

Close iFileNum

'Now we loop through each item in the
'array and add it to our listbox
For iCounter = 0 To UBound(maArray)
   List1.AddItem maArray(iCounter)
Next

ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what's wrong with E-E??
Thanks that is what I am after.

A better description of what and how.

And thanks to all of you who added comments. and answering my question "Is there any reason I should not do this."

I figured it was way to easy.

I will see if I can get this method to work for me!

Thanks to viktornet for trying to answer it but for someone who has never don this before (Me) it was not very clear.

Is there any way I can give him some points anyway?
Avatar of wadeg

ASKER

Arrg! now the answer dissapeared on me!

Oh well I have some idea what direction to go!
Avatar of wadeg

ASKER

wadeg, as the guys have mentioend already it saves the array into the registry, and it is not a good idea to saves a couple of megs in your registry... the registry is only for settions and options, etc... (it is still saved to a file, but that's a different story...)... okay, i think my method would've worked the best... wish ya luck...

..-=ViKtOr=-..
they have the messages all messed up!

(who sent what)


Avatar of wadeg

ASKER

The answer was Waty's but it appeared under my name. Every message is moved up one position.
Thanks as long as I do not have to parse a HUGE text file I am happy!
Avatar of wadeg

ASKER

hey guys, just so you know this is viktornet ;-))

If I gave the correct answer why didn't you accept my answer and did Waty's instead??

..-=ViKtOr=-..
He described it better. (this is wadeg)

I will give you point if I can

just aswer my next question

"for viktornet"

You earned it anyway but so did waty
Avatar of wadeg

ASKER

wadeg, where the hell is his explanation??

-vik-
This is messed up. (this is wadeg)

He gave me reasons why not to use the regestry. as di all of you. and he explained what your solution does + gave me yet another way to do it. I gave Waty the points simply for the efort and a good explination. I will give you 50 points for the correct answer!

thanks all!
Avatar of wadeg

ASKER

(this is viktornet)

wadeg, as you can see the binary explanation wasn't waty's, but was rather mine... it appeared to be his 'cuz of this messed up thread... and I think write and get were the easiest solutions you can get... a single line of code instead of dozens of those...

..-=ViKtOr=-..
It still will not work! (wadeg)

I tried write and put and print nothing will just write the array!

I am getting flustered!