473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

help using arrays in a function

Hello,

I have a rather simple function I've been using in 7.3.4

version
------------------------------------------------------------------------
-----------------
PostgreSQL 7.3.4 on i686-pc-cygwin, compiled by GCC gcc (GCC) 3.2
20020927 (prerelease)
(1 row)

This compares an integer to all elements in an integer array and looks
for a match

CREATE OR REPLACE FUNCTION public.array_element_compare(_int4, int4)
RETURNS text AS
'
DECLARE
a ALIAS FOR $1;
b ALIAS FOR $2;
i integer;
status text;
BEGIN
i := 1;
status := ''false'';
WHILE a[i] LOOP
IF a[i] = b THEN
status := ''true'';
END IF;
i := i+1;
END LOOP;
RETURN status;
END;'
LANGUAGE 'plpgsql';

It works find

germinate1.5 =# select array_element_compare('{100,92,82}', 82);
array_element_compare
-----------------------
true
(1 row)

germinate1.5 =# select array_element_compare('{100,92}', 82);
array_element_compare
-----------------------
false
(1 row)

I'm now testing our database on 7.4 and get an error with this function
and I can't figure out how to fix it.

version
------------------------------------------------------------------------
-------------
PostgreSQL 7.4 on i686-pc-cygwin, compiled by GCC gcc (GCC) 3.3.1
(cygming special)
(1 row)

germinate1.5 =# select array_element_compare('{100, 92, 82}', 82);
ERROR: invalid input syntax for type boolean: "100"
CONTEXT: PL/pgSQL function "array_element_compare" line 9 at while

Can anyone point me in the right direction?

Thanks much for any help,
Jennifer



************************************************** ***************
DISCLAIMER:

This email is from the Scottish Crop Research Institute, but the views expressed by the sender are not necessarily the views of SCRI and its subsidiaries. This email and any files transmitted with it are confidential to the intended recipient at the e-mail address to which it has been addressed. It may not be disclosed or used by any other than that addressee.
If you are not the intended recipient you are requested to preserve this confidentiality and you must not use, disclose, copy, print or rely on this e-mail in any way. Please notify ma**@scri.sari.ac.uk quoting the name of the sender and delete the email from your system.

Although SCRI has taken reasonable precautions to ensure no viruses are present in this email, neither the Institute nor the sender accepts any responsibility for any viruses, and it is your responsibility to scan the email and the attachments (if any).

Nov 22 '05 #1
2 1846
"Jennifer Lee" <jl**@scri.sari.ac.uk> writes:
WHILE a[i] LOOP I'm now testing our database on 7.4 and get an error with this function
and I can't figure out how to fix it.
ERROR: invalid input syntax for type boolean: "100"
CONTEXT: PL/pgSQL function "array_element_compare" line 9 at while


plpgsql now enforces that the test expression of IF, WHILE, etc must be
a boolean. The above is not.

This is pretty poor coding practice anyway, since even before 7.4 it
would have given wrong answers for arrays containing zeroes or arrays
whose lower index bound is not 1. I'd suggest using the array_lower and
array_upper functions to determine the valid range of subscripts.
Something like

FOR i IN array_lower(a,1) .. array_upper(a,1) LOOP

should work.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #2
Jennifer Lee wrote:
WHILE a[i] LOOP


This problem has nothing to do with arrays, per se. PL/pgSQL no longer
assumes it can coerce an integer constant to a boolean. Instead make the
above line look like this:

WHILE a[i] IS NOT NULL LOOP
BTW, in 7.4 you could more easily get the same result like this:

regression=# select 82 = any ('{100,92,82}');
?column?
----------
t
(1 row)

regression=# select 82 = any ('{100,92}');
?column?
----------
f
(1 row)

See:
http://www.postgresql.org/docs/curre...mparisons.html

HTH,

Joe

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
1
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
8
by: inkexit | last post by:
I am a very amatuer c++ programmer and a somewhat accomplished composer. I am trying to write some code that creates 'self similar' melodies from a base melody the user inputs. This musical idea...
27
by: ruel loehr | last post by:
Hey guys, I am looking for some insight: Given two sorted arrays of integers A and B, where array B has enough extra room in it to hold the contents of both A and B. Merge array A and B...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.