Negative indexes in arrays for additional access-possibility

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Negative indexes in arrays for additional access-possibility

Post 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? :idea:
PB 4.30

Code: Select all

onErrorGoto(?Fred)
r_hyde
Enthusiast
Enthusiast
Posts: 155
Joined: Wed Jul 05, 2006 12:40 am

Post by r_hyde »

Reminds me of Python. I like it :D
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Yes, it's a very comfortable way to access arrays. Me reminds it of Perl.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

and me Python.

in Python it also works on strings (as it is also an array... of chars)

>>"Hello World"[-5]
>>"World"
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
r_hyde
Enthusiast
Enthusiast
Posts: 155
Joined: Wed Jul 05, 2006 12:40 am

Post 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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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....
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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?
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
r_hyde
Enthusiast
Enthusiast
Posts: 155
Joined: Wed Jul 05, 2006 12:40 am

Post 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.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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? :lol: 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! :P
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Post Reply