Thread: How to remove n-first elements from array?

How to remove n-first elements from array?

From
aasat
Date:
Hi,

I have array for example

do $$
declare
  v_arr integer[] := array[1, 2, 3, 4, 5];
begin
end;
$$

I want to remove 2 first element and get array with non-changed indexes
(subscript)

new array := '[3:5]={3, 4, 5}'::integer[]

It is possible in pl/pgsql?



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-remove-n-first-elements-from-array-tp5736765.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: How to remove n-first elements from array?

From
aasat
Date:
I found solution

do $$
declare
  v_array integer[] := array[1,2,3,4,5];
  v_array2 integer[];
  v_delete_elements integer :=2;
  v_new_index integer;
begin
  v_new_index := array_lower(v_array, 1) + v_delete_elements;

  v_array2 := array_fill(v_array[v_new_index], ARRAY[1], ARRAY[v_new_index])
||   v_array[v_new_index+1:array_upper(v_array, 1)];
  raise notice '%', v_array2;
end;
$$

NOTICE:  [3:5]={3,4,5}




--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-remove-n-first-elements-from-array-tp5736765p5736866.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: How to remove n-first elements from array?

From
Merlin Moncure
Date:
On Mon, Dec 17, 2012 at 3:27 AM, aasat <satriani@veranet.pl> wrote:
> I found solution
>
> do $$
> declare
>   v_array integer[] := array[1,2,3,4,5];
>   v_array2 integer[];
>   v_delete_elements integer :=2;
>   v_new_index integer;
> begin
>   v_new_index := array_lower(v_array, 1) + v_delete_elements;
>
>   v_array2 := array_fill(v_array[v_new_index], ARRAY[1], ARRAY[v_new_index])
> ||   v_array[v_new_index+1:array_upper(v_array, 1)];
>   raise notice '%', v_array2;
> end;
> $$
>
> NOTICE:  [3:5]={3,4,5}

here's a solution that's a bit more general:
create or replace function remove_front(a anyarray, nkeep int) returns
anyarray as
$$
  select a[array_upper(a, 1) - (nkeep - 1):array_upper(a, 1)];
$$ language sql immutable;

merlin


Re: How to remove n-first elements from array?

From
aasat
Date:
Thank you!

More general, but that lose bounds of array

Result is

NOTICE:  {3,4,5}

but I want

NOTICE:  [3:5]{3,4,5}





--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-remove-n-first-elements-from-array-tp5736765p5736894.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: How to remove n-first elements from array?

From
Merlin Moncure
Date:
On Mon, Dec 17, 2012 at 9:02 AM, aasat <satriani@veranet.pl> wrote:
> Thank you!
>
> More general, but that lose bounds of array
>
> Result is
>
> NOTICE:  {3,4,5}
>
> but I want
>
> NOTICE:  [3:5]{3,4,5}

why do you want that? IMNSHO custom lower bounds arrays is a misfeature.

merlin


Re: How to remove n-first elements from array?

From
aasat
Date:
I use it for page hits aggregations, It's very fast, my arrays contains no
more than 1000 elements (array_upper-array_lower <=1000) and array bounds is
mapped to time.





--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-remove-n-first-elements-from-array-tp5736765p5736933.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.