By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

array faults?

P: n/a
Using arrays I came across some strangenesses. Probably this is well
known, but I couldn't find mentions of it.

I am under the impression that this should be an error. Am I wrong?

EXAMPLE 1:
maint=# select ('{{1,2,3},{4,5,6}}'::int[])[1][1:3];
int4
-----------
{{1,2,3}}
(1 row)

Shouldn't this have been just {1,2,3} ?

EXAMPLE 2:
maint=# select ('{{1,2,3},{4,5,6}}'::int[])[2][1:3];
int4
-----------
{{1,2,3},{4,5,6}}
(1 row)

Shouldn't this have been just {4,5,6} (or maybe {{4,5,6}} accepting the
result of example 1) ?
I am trying to do sth like:
maint=# select 1 = any ('{{1,2,3},{4,5,6}}'::int[])[2][1:3];
But that is not working for obvious reasons. This makes arrays pretty
broken for me.

Am I missing anything obvious?

Regards,

David Helgason
Over the Edge Entertainments
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
David Helgason <da***@uti.is> writes:
EXAMPLE 1:
maint=# select ('{{1,2,3},{4,5,6}}'::int[])[1][1:3];
int4
-----------
{{1,2,3}}
(1 row) Shouldn't this have been just {1,2,3} ?
Nope. It's equivalent to (...)[1:1][1:3]. See section 8.10.3 "Accessing
Arrays" in the current documentation. Note in particular where it says

An array subscripting operation is always taken to represent an array
slice if any of the subscripts are written in the form lower:upper. A
lower bound of 1 is assumed for any subscript where only one value is
specified, ...
Shouldn't this have been just {4,5,6} (or maybe {{4,5,6}} accepting the
result of example 1) ?


See above. You need to write [2:2] not [2].

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.