Counting elements of an array

Started by Renaud Tthonnartabout 25 years ago6 messagesgeneral
Jump to latest
#1Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr

Good morning all,

I would like to know how I can get the number of elements of an array.

regards,
Renaud THONNART

#2Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr
In reply to: Renaud Tthonnart (#1)
Re: Counting elements of an array

Christopher Sawtell wrote:

On Thu, 01 Mar 2001 21:53, you wrote:

Good morning all,

I would like to know how I can get the number of elements of an array.

create function array_element_count(_int4) returns integer as '
declare
a alias for $1;
i integer;
begin
i := 1;
while a[i] loop
i := i+1;
end loop;
return i-1;
end;' language 'plpgsql' with(isstrict,iscachable);

Thanks a lot
Regards, Renaud THONNART

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Renaud Tthonnart (#1)
Re: Counting elements of an array

Renaud Tthonnart <thonnart@amwdb.u-strasbg.fr> writes:

I would like to know how I can get the number of elements of an array.

There is a function that returns an array's dimensions as a text string:

regression=# select array_dims( '{1,2,3}'::int[] );
array_dims
------------
[1:3]
(1 row)

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

regards, tom lane

#4Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr
In reply to: Renaud Tthonnart (#1)
Re: Counting elements of an array

Tom Lane wrote:

Renaud Tthonnart <thonnart@amwdb.u-strasbg.fr> writes:

I would like to know how I can get the number of elements of an array.

There is a function that returns an array's dimensions as a text string:

regression=# select array_dims( '{1,2,3}'::int[] );
array_dims
------------
[1:3]
(1 row)

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

regards, tom lane

Thanks very much Tom, you have helped me a lot.
Renaud THONNART

#5Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr
In reply to: Renaud Tthonnart (#1)
Re: Counting elements of an array

Tom Lane wrote:

Renaud Tthonnart <thonnart@amwdb.u-strasbg.fr> writes:

I would like to know how I can get the number of elements of an array.

There is a function that returns an array's dimensions as a text string:

regression=# select array_dims( '{1,2,3}'::int[] );
array_dims
------------
[1:3]
(1 row)

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

regards, tom lane

Ok Tom, but if I have a table (for example aaa) that contains an array (for
example vector)

The function that you have spoken to me doesn't work:

select array_dims(vector) from aaa;
or
select array_dims(vector :: int[]) from aaa;

The result is :

array_dims
------------

(3 row)

There isn't any parse error but it don't work.

Do I have badly understand what you have explain me?

Thank you,
Renaud THONNART

#6Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr
In reply to: Renaud Tthonnart (#1)
Re: Counting elements of an array

Sorry!!
It works very well!

Renaud THONNART

Show quoted text

Renaud Tthonnart <thonnart@amwdb.u-strasbg.fr> writes:

I would like to know how I can get the number of elements of an array.

There is a function that returns an array's dimensions as a text string:

regression=# select array_dims( '{1,2,3}'::int[] );
array_dims
------------
[1:3]
(1 row)

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

regards, tom lane