Обсуждение: Array shift equivalent?

Поиск
Список
Период
Сортировка

Array shift equivalent?

От
Michael Glaesemann
Дата:
I'm trying to shift the first element off of an array, but I'm just
not getting it. I'm not necessarily interested in saving the rest of
the array. i just want to get the first element, which in this case
is another array.

# select * from arrays;
this_array_id |                    this_array
---------------+---------------------------------------------------
              1 | {{foo,bar,baz},{afoo,abar,abaz},{bfoo,bbar,bbaz}}
(1 row)

I'd like to get {foo,bar,baz}.

My first try:

# select this_array[1] from arrays;
this_array
------------
(1 row)

# select this_array[1][1:3] from arrays;
    this_array
-----------------
{{foo,bar,baz}}
(1 row)

This last one is close, but it's nested:

# select array_dims(this_array[1][1:3]) from arrays;
array_dims
------------
[1:1][1:3]
(1 row)

I'd like to get just a [1:3] array.

What am I missing? Thanks for any assistance.

Michael Glaesemann
grzm seespotcode net



Re: Array shift equivalent?

От
Tom Lane
Дата:
Michael Glaesemann <grzm@seespotcode.net> writes:
> I'm trying to shift the first element off of an array, but I'm just
> not getting it. I'm not necessarily interested in saving the rest of
> the array. i just want to get the first element, which in this case
> is another array.

No, it isn't.  What you've got is a 2-D array.  I don't think there
is an operation that will produce a 1-D subset --- the array slice
subscript operation produces something that's smaller but still has
the same number of dimensions.

            regards, tom lane

Re: Array shift equivalent?

От
Michael Glaesemann
Дата:
On Dec 7, 2006, at 0:26 , Tom Lane wrote:

> Michael Glaesemann <grzm@seespotcode.net> writes:
>> I'm trying to shift the first element off of an array, but I'm just
>> not getting it. I'm not necessarily interested in saving the rest of
>> the array. i just want to get the first element, which in this case
>> is another array.
>
> No, it isn't.  What you've got is a 2-D array.  I don't think there
> is an operation that will produce a 1-D subset --- the array slice
> subscript operation produces something that's smaller but still has
> the same number of dimensions.

Thanks for the explanation and confirmation. I end up iterating over
the elements of the subarray, so I worked around it by iterating over
this_array[1][i].

Michael Glaesemann
grzm seespotcode net