Thread: Add value in an Array

Add value in an Array

From
"Jerome Chochon"
Date:
Hi all.
 
I am a french student working on a PostgreSQL database.
So i want to create a view which use a fonction (write in PLPGSQL) .
This function will take a colum from a table (the column is integer[] based) and add a value in the array.
So i dont find an operator to make this command.
 
Someone can help me ?
 
Best regards.
 
Jérôme Chochon

Re: Add value in an Array

From
Mikail Majorov
Date:
On Thu, 25 Jul 2002 13:48:58 +0200
"Jerome Chochon" <jerome.chochon@ensma.fr> wrote:

> Hi all.
>
> I am a french student working on a PostgreSQL database.
> So i want to create a view which use a fonction (write in PLPGSQL) .
> This function will take a colum from a table (the column is integer[] based) and add a value in the array.
> So i dont find an operator to make this command.
>
> Someone can help me ?

For example.....

CREATE FUNCTION add_array(text[], text) RETURNS text[] AS '
    $_[0] =~ s/[{}"]//g; $_[1] =~ s/[{}"]//g;
    my %hs = ();
    foreach my $c (split(/,/, $_[0])) { $hs{$c} = 1; }
    if ($_[1] ne "") { $hs{$_[1]} = 1; }
    return "{" . join(",", (keys %hs)) . "}";
' LANGUAGE 'plperl';

CREATE OPERATOR + (leftarg=text[], rightarg=text, procedure=add_array);

CREATE FUNCTION del_array(text[], text) RETURNS text[] AS '
    $_[0] =~ s/[{}"]//g; $_[1] =~ s/[{}"]//g;
    my %hs = ();
    foreach my $c (split(/,/, $_[0])) { $hs{$c} = 1; }
    if ($_[1] ne "") { delete $hs{$_[1]}; }
    return "{" . join(",", (keys %hs)) . "}";
' LANGUAGE 'plperl';

CREATE OPERATOR - (leftarg=text[], rightarg=text, procedure=del_array);

--
С уважением,
Михаил Майоров
Таганрогский Узел ЭлектроСвязи
+7 8634 362563
+7 8634 383242 fax only


Re: Add value in an Array

From
"Jerome Chochon"
Date:
Thanks but i want to use only PLPGSQL or SQL.
Or perhaps there is a native function in PostgreSQL (like dim_arrays).

Someone have another solution ?


----- Original Message -----
From: "Mikail Majorov" <mik@nix.org.ru>
To: "Jerome Chochon" <jerome.chochon@ensma.fr>
Cc: <pgsql-general@postgresql.org>
Sent: Thursday, July 25, 2002 1:47 PM
Subject: Re: [GENERAL] Add value in an Array


> On Thu, 25 Jul 2002 13:48:58 +0200
> "Jerome Chochon" <jerome.chochon@ensma.fr> wrote:
>
> > Hi all.
> >
> > I am a french student working on a PostgreSQL database.
> > So i want to create a view which use a fonction (write in PLPGSQL) .
> > This function will take a colum from a table (the column is integer[]
based) and add a value in the array.
> > So i dont find an operator to make this command.
> >
> > Someone can help me ?
>
> For example.....
>
> CREATE FUNCTION add_array(text[], text) RETURNS text[] AS '
>     $_[0] =~ s/[{}"]//g; $_[1] =~ s/[{}"]//g;
>     my %hs = ();
>     foreach my $c (split(/,/, $_[0])) { $hs{$c} = 1; }
>     if ($_[1] ne "") { $hs{$_[1]} = 1; }
>     return "{" . join(",", (keys %hs)) . "}";
> ' LANGUAGE 'plperl';
>
> CREATE OPERATOR + (leftarg=text[], rightarg=text, procedure=add_array);
>
> CREATE FUNCTION del_array(text[], text) RETURNS text[] AS '
>     $_[0] =~ s/[{}"]//g; $_[1] =~ s/[{}"]//g;
>     my %hs = ();
>     foreach my $c (split(/,/, $_[0])) { $hs{$c} = 1; }
>     if ($_[1] ne "") { delete $hs{$_[1]}; }
>     return "{" . join(",", (keys %hs)) . "}";
> ' LANGUAGE 'plperl';
>
> CREATE OPERATOR - (leftarg=text[], rightarg=text, procedure=del_array);
>
> --
> С уважением,
> Михаил Майоров
> Таганрогский Узел ЭлектроСвязи
> +7 8634 362563
> +7 8634 383242 fax only
>