Checking empty array

Started by Alexis Beuraudover 18 years ago4 messagesgeneral
Jump to latest
#1Alexis Beuraud
alexis@siatel.com

Dear all,

What is the correct way to check whether an array is empty?
I have an array which I initialize with '{}' and then do som array_append
under some circonstances. I would like to know whether the array is empty at
the end of the function. I have not found the answer in the help page nor on
the web. I use the keyword isnull but that does not seem to work in all the
cases.
I would appreciate if someone could put this piece of information into the
Postgres documentation.

Here is what I do, in pseudo-code:

myarray varchar[];
myarray := '{}';

FOR ___ LOOP
IF (___) THEN
myarray := array_append(myarray, somestuff);
END IF;
END LOOP;

-- Is my array empty now?
IF (myarray isnull) THEN
__
END IF;

Regards,
Alexis Beuraud

#2Richard Huxton
dev@archonet.com
In reply to: Alexis Beuraud (#1)
Re: Checking empty array

Alexis Beuraud wrote:

Dear all,

What is the correct way to check whether an array is empty?

-- Is my array empty now?
IF (myarray isnull) THEN

An empty array isn't null (unknown), it's empty.

Try enquiring about it's size:
SELECT array_dims('{}'::integer[]) is null;
?column?
----------
t

You could use array_lower/upper instead too.

--
Richard Huxton
Archonet Ltd

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Alexis Beuraud (#1)
Re: Checking empty array

2007/10/30, Alexis Beuraud <alexis@siatel.com>:

Dear all,

What is the correct way to check whether an array is empty?
I have an array which I initialize with '{}' and then do som array_append
under some circonstances. I would like to know whether the array is empty at
the end of the function. I have not found the answer in the help page nor on
the web. I use the keyword isnull but that does not seem to work in all the
cases.
I would appreciate if someone could put this piece of information into the
Postgres documentation.

Here is what I do, in pseudo-code:

myarray varchar[];
myarray := '{}';

FOR ___ LOOP
IF (___) THEN
myarray := array_append(myarray, somestuff);
END IF;
END LOOP;

-- Is my array empty now?
IF (myarray isnull) THEN
__
END IF;

IF (array_upper(myarray,1) is null) THEN
....
END IF;

Regards
Pavel Stehule

Show quoted text

Regards,
Alexis Beuraud

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

#4Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Alexis Beuraud (#1)
Re: Checking empty array

Alexis Beuraud wrote:

What is the correct way to check whether an array is empty?

Here is what I do, in pseudo-code:

-- Is my array empty now?
IF (myarray isnull) THEN

Try this:

IF array_lower(myarray, 1) IS NULL THEN ... END IF

Yours,
Laurenz Albe