Simplified array/linked list filling

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Simplified array/linked list filling

Post 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
~I see one problem with your reasoning: the fact is thats not a chicken~
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You could use the 'Data' which could help a lot in your case.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post 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
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post 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.
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: Simplified array/linked list filling

Post 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}
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Kaeru Gaman wrote:

Code: Select all

myColor = $4284f0
What exactly does this help if I need an array?
Good programmers don't comment their code. It was hard to write, should be hard to read.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post 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?
~I see one problem with your reasoning: the fact is thats not a chicken~
Post Reply