Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How convert array elements to rows?

Status
Not open for further replies.

sigra

Programmer
Nov 3, 2008
1
SE
I need to convert array elements to rows. Suppose that I have the array {5,77,39,19}. I need it as a table with the index in one column and the element in the other column:
Code:
index | element
------+--------
    1 |       5
    2 |      77
    3 |      39
    4 |      19
Which command does this?
 
Do you need a data structure, a collection type or do you need a GUI-component to display your array?

The index is already part of your array: array[2]=39 if you don't insist in indexing your rows from 1...len, but can use 0...(len-1).

Else you add 1 to the index and get the row.

Or explain your problem in more detail.

don't visit my homepage:
 
Like that

rski=> select i, (ARRAY[11,22,33]) from
generate_series(1,array_upper(ARRAY[11,22,33],1)) i;
i | array
---+-------
1 | 11
2 | 22
3 | 33
(3 rows)

If you know the dimension of a table you can replace array_upper with it.
 
Sorry I forget the CODE tags. It looks better


Code:
rski=> select i, (ARRAY[11,22,33])[i] from
          generate_series(1,array_upper(ARRAY[11,22,33],1)) i;
 i | array
---+-------
 1 |    11
 2 |    22
 3 |    33
(3 rows)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top