Page 1 of 1
Negative indexes in arrays for additional access-possibility
Posted: Thu Mar 29, 2007 7:03 pm
by AND51
Hello!
What do you think of negative indexes for arrays?
In Perl, this feature does exist. It makes it possible to access the array bidirectionally:
Code: Select all
Dim names.s(5)
; filling the array...
names(0)="Andre"
names(1)="Peter"
names(2)="Tim"
names(3)="Tom"
names(4)="Fred"
; Debugging
Debug names(3) ; Tim
Debug names(5) ; Fred
Debug names(0) ; Andre
Debug names(-1) ; Fred
Debug names(-2) ; Tom
Debug names(-3) ; Tim
; etc...
; Array index out of bound when trying
Debug names(-6)
Debug names(7)
What do you think about this idea?

Posted: Thu Mar 29, 2007 7:40 pm
by r_hyde
Reminds me of Python. I like it

Posted: Thu Mar 29, 2007 8:25 pm
by AND51
Yes, it's a very comfortable way to access arrays. Me reminds it of Perl.
Posted: Thu Mar 29, 2007 8:35 pm
by Flype
and me Python.
in Python it also works on strings (as it is also an array... of chars)
>>"Hello World"[-5]
>>"World"
Posted: Fri Mar 30, 2007 6:02 am
by Kaeru Gaman
I was trying to write a proc for it, but it's not that easy...
I think it is possible, because you can get the UBound by LPeek(*array-8),
so you just need the -Index added...
but anyways... what is the use of it?
there are a lot of features connected with the different philosophy of other programming languages...
Pearl is a Database-Scripting, isn't it?
Phyton is a 3D-Scripting...
PB is a Compiler-BASIC....
and the question remains:
what would you need it for?
in my 24(!) years of programming even the idea of using negative indices never ever merestly crossed my mind.
Posted: Fri Mar 30, 2007 7:36 am
by r_hyde
I don't mean to insult you, Kaeru Gaman, but it always strikes me as odd when I hear that sort of comment about a feature request, and this applies not only to PureBasic but to all types of software and programming languages. Just because you can't imagine how you might use feature x, it does not stand to reason that there is no use at all for feature x.
Indexing arrays using negative indices is, for some people, very intuitive and useful. You are free to continue not using it if it ever happens to be implemented.
Also, it should be noted that Perl, while useful for database scripting, is much more than a database scripting language. Just as Python, while used as the scripting language in a handful of 3D apps, is much more than a 3D scripting language.
Posted: Fri Mar 30, 2007 8:20 am
by Kaeru Gaman
sorry, don't get me wrong...
I'm not negating a request, but only asking for the use.
if I think about negative indices for an array, then I would request totally index wrapping,
which would mean e.g. to get Array(30) when calling an Array(390) that is Dimensioned Array(360) [in PB Array(359)]
with complete wrapping I can see the use, e.g. having a sinus array and no need to calculate the wrapping of the index beforehand....
Posted: Fri Mar 30, 2007 10:21 am
by Trond
Indexing arrays using negative indices is, for some people, very intuitive and useful. You are free to continue not using it if it ever happens to be implemented.
But even if he doesn't use it, his programs will get a severe speed penalty as all array indices must be checked for their sign.
Posted: Fri Mar 30, 2007 11:17 am
by AND51
PeeL(@array()-8) is just a workaround, Kaeru! This method to find out the length of an array might change in future versions of PB, and then we would have to re-code this workaorung.
Negative indexes would be a fine method to also access arrays with an unkown length (as my perldocumentation says). Moreover, you could easyly write a QickSort function, using negative indexes even without knowing the length of an Array.
I don't think, that there will be a speed loss which have an impact on your PB apps, even if you're not using this feature. An example can be found here
http://www.purebasic.fr/english/viewtopic.php?t=26154 - who can guarantee me, that including the extra-flag does NOT lead to speed loss, just because I'm sure that I'll never use this feature? So please, come on! Don't be so narrow-minded!
All in all, I totally agree to r_hyde. Implementing a new feature does not mean that you
must use it then.
Posted: Fri Mar 30, 2007 12:26 pm
by Trond
I don't think, that there will be a speed loss which have an impact on your PB apps, even if you're not using this feature.
If it's turned on for all arrays then there will of course be. But maybe it could be implemented for only the arrays you specify?
Posted: Fri Mar 30, 2007 12:32 pm
by AND51
> But maybe it could be implemented for only the arrays you specify?
But this would make it more complicated for me, you and Fred:
- Fred has got double work
- I must always switch this option on (maybe in the compiler options)
- You must always switch this option on or off (e. g. when you're testing someone else's code)
> If it's turned on for all arrays then there will of course be.
Sorry, my mistake. I meant, that the impact will not be that great IMHO. But i can only guess.
Posted: Fri Mar 30, 2007 1:26 pm
by Kaeru Gaman
> Don't be so narrow-minded!
hey, boy, did you get a new dictionary for birthday?
Trond wrote:I don't think, that there will be a speed loss which have an impact on your PB apps, even if you're not using this feature.
If it's turned on for all arrays then there will of course be. But maybe it could be implemented for only the arrays you specify?
exactly that is the point.
PB is that fast because it is
not overloaded with every hype you can imagine.
Posted: Fri Mar 30, 2007 3:02 pm
by Trond
> If it's turned on for all arrays then there will of course be.
Sorry, my mistake. I meant, that the impact will not be that great IMHO. But i can only guess.
It will be one
cmp register, 0, one
jl that is not triggered and one
jmp for EVERY array access if it gets implemented optimally.
Posted: Sat Mar 31, 2007 5:15 am
by r_hyde
Trond makes a very good point about the speed. No problem, just implement it as a different kind of array. Dim and index it using curly braces or something. Speed is important, but let's remember that it's not always the most important thing. Only a small percentage of the programs I've written have been bound by the speed of my code - most of them are bound by the speed of the user, or the speed of a network connection. I suspect the same is true for a lot of developers here.
I'd be happy to see this feature implemented, but I suppose it's easy enough to do without it.
Posted: Sat Mar 31, 2007 10:49 am
by AND51
Hey, great idea that you're discussing a possible imlementation-solution. That make's Fred work easier!
@ r_hyde: Hm... I'm afraid... Can you read my mind?

I give my full acknowledge to you!
> hey, boy, did you get a new dictionary for birthday?
Hm... What do you mean? My dictionary is dict.leo.org!
