Page 1 of 1

Simplified array/linked list filling

Posted: Wed Apr 04, 2007 11:20 pm
by Killswitch
When I'm involved in a big project I often find it nessacary to fill an array with a large amount of data that I can't generate on-the-fly so I have to spend a lot of time filling the arrays. I've not really encountered this problem with linked lists, but the functionality is fairly applicable to them as well.

Ok, here's an example of what I'm tired of doing:

Code: Select all

  Error.s(0)="You have pressed esc."
  Error.s(1)="something"
  ...
  Error.s(n)="oh my god, I hate doing this"
And here's what I suggest would be a good replacement:

Code: Select all

  Error.s(0){"You have pressed esc","something",...,"oh my god, I hate doing this"}
Generalised:

Code: Select all

  Array( StartIndex.l){1,2...}
A precompiler could handle this pretty well, I'm sure (in fact, I may just hammer one out) but I - and I'm sure everyone else does - really hate using non-standard language features because it's a pain in sharing source/setting up precompilers on every computer you code on/keeping precompilers up-to-date.

Thanks,

Killswitch

Posted: Wed Apr 04, 2007 11:39 pm
by Fred
You could use the 'Data' which could help a lot in your case.

Posted: Wed Apr 04, 2007 11:46 pm
by Killswitch
That's always a possibility, I just think that my proposed syntax is a little more elegant (although it's not really basic).

Still, you're the PB God, Fred :P

Posted: Thu Apr 05, 2007 10:05 am
by Kaeru Gaman
what about filling the array from a textfile within a loop?

Code: Select all

Dim Bla$(8)

If ReadFile(0,"textfile.txt")
  For n=0 To 8
    Bla$(n) = ReadString(0,#PB_Ascii)
  Next
  CloseFile(0)
EndIf

For t=0 To 8
  Debug Bla$(t)
Next
textfile.txt is:

Code: Select all

this is a test
does it work as expected?
-------------------------
what's the difference in an apple?
It's a telephone pole
because motorcycles don't have doors,
elephants don't eat icecream,
and an egg when broken
must stay the same crushed egg forever.
also for different structured datasets, a predefined extern file is a good possibility.
and of course there are ways to protect the data to have the user not changed it easily.

of course, you can do it the same way via DataSection, it a good solution, too.

...but direct elementwise assignment within the code is not a good solution....


your idea reminds me of C, but it's only practicable if you could use multiple lines.
to write a complete Array in one line would be a PITA.....
better use DataSection then, you could give it a table-form there, too.

Posted: Thu Apr 05, 2007 10:50 am
by GedB
Killswhich,

You might find this old discussion interesting: http://www.purebasic.fr/english/viewtop ... torder=asc

NetMaestro and I throw about ideas for quickly loading dictionarys using IncludeBinary() and an Index.

Posted: Thu Apr 05, 2007 12:00 pm
by Killswitch
Loading from a text file was always a possibility, I just dislike the idea of the overhead involved (however small) and the fact that the file can easily be modified.

Posted: Thu Apr 05, 2007 12:21 pm
by Kaeru Gaman
Killswitch wrote:... I just dislike ... the fact that the file can easily be modified.
implementing an encryption is really easy....
I posted some little procs a while ago in the german forum...
http://www.purebasic.fr/german/viewtopic.php?t=8807

....maybe I should work it over again a bit, but it's an example how easy textdata can be encrypted.
after that, they are saver than raw-data included in the exe, that can be modified by any HEX-editor.

Posted: Thu Apr 05, 2007 12:23 pm
by GedB
Killswhich,

IncludeBinary binds the text file to your binary at compile time, so no runtime overhead is incurred.

Compress the text and it reduces load time and stops people from seeing the data when the open the binary in a text editor.

Read the thread. Netmaestro includes the entire scrabble dictionary within a small dll that can be searched in seconds.

This is the type of stuff thats considered guru level meditation in other languages. In PB its easy.

Re: Simplified array/linked list filling

Posted: Thu Apr 05, 2007 1:15 pm
by traumatic
I second this request.

Often Datasections aren't really convenient to use, just think of simple things
like filling vector- or color-arrays...

Code: Select all

myColorRGB() = {$f0, $84, $42}

Posted: Thu Apr 05, 2007 2:02 pm
by Kaeru Gaman
@traumatic

hm... why not

Code: Select all

myColor = $4284f0

don't get me wrong... I think it might be a nice feature...
but to take full advantage of it we also need multilines, or it's just not really practicable...

Posted: Thu Apr 05, 2007 2:22 pm
by traumatic
Kaeru Gaman wrote:

Code: Select all

myColor = $4284f0
What exactly does this help if I need an array?

Posted: Thu Apr 05, 2007 7:52 pm
by Killswitch
@GedB

Are you sure it doesn't add overhead? I mean, you have to actually load the file, then open it, then loop through it and then write all of the data to the array you're filling - surely simply filling the array by hand is quicker, even if its marginally quicker?