String arrays

Just starting out? Need help? Post your questions and find answers here.
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

String arrays

Post by geoff »

I want to create a string array a$(100000) and populate it with strings of
varying length. Most have just a few characters but several have many
thousands. Some of these strings contain chr(0).

This is easy in most Basics but PB does not allow chr(0) within strings.

Is there a simple alternative?
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

I don't *think* there would be any way to include a NULL character within a string since NULL indicates end-of-string. I can suggest maybe escaping the NULLs (rather, replacing the NULLs with something) and re-assembling then when they're used?

Good luck!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

Post by geoff »

Thanks for the suggestion Karbon but it won't work here because the
strings contain all 256 possible characters (they are encrypted)

Anyone else have a neat solution?
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Maybe you can do an array of pointers instead and allocate memory for each entry in the array to hold the string data..
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

Post by geoff »

Maybe you can do an array of pointers instead and allocate memory for each entry in the array to hold the string data..
Yes, that's probably the best way to do it.

But, this is equivalent to writing your own pseudo-string routines. Sadly, the PB string functions would not work on these pseudo-strings so you would have to write versions of them as well.

Alternatively, you could manipulate the memory bytes directly using pointers. But the main advantage of using a high level language is to avoid this sort of thing because it is slow to write and error prone.

This leaves me thinking that PB strings should allow null characters. You could still have a null as the last character if you wanted to, for compatibility with C, for example

Fred, why not allow nulls in strings like other Basics?
Basic is clearly superior to C in this respect, so why do it the C way?

Can anyone see a problem with this idea?
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

This leaves me thinking that PB strings should allow null characters. You could still have a null as the last character if you wanted to, for compatibility with C, for example

Fred, why not allow nulls in strings like other Basics?
Basic is clearly superior to C in this respect, so why do it the C way?

Can anyone see a problem with this idea?
Yea, backwards compatibility would probably be the single biggest problem :-)

Regardless, there has to be something to indicate end-of-string and NULL is pretty widely used an accepted as that character.. However toss this into the feature request forum and see what happens!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Gantry
User
User
Posts: 20
Joined: Tue Jun 03, 2003 6:24 am
Location: Sweden

Post by Gantry »

Moving away from zero terminated strings would, I imagine, cause Fred working a long time fixing something that's not broken. Try to call the API with a string like that. Is it possible to convince the API that zero is not the termination character? I don't know.

Sure, it could be neat being able to include a NULL character but what's the cost (rewriting the compiler) vs the benefit (sometimes useful)?

Then again, I have been known to be wrong from time to time...

Have a nice day all!

/Gantry
*** In the end we will remember not the words of our enemies, but the silence of our friends. (Martin Luther King Jr.) ***
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

Post by aszid »

hmmmm.....

well, i think it would be a cool idea to have strings that could contain nulls, but the best approach would probably be a new type of string, rather than changing how current strings work.

something like nullstring.ns perhaps? i know for certain that it would be very usefull to me.
--Aszid--

Making crazy people sane, starting tomorrow.
Post Reply