Thread: [REPOST] plpgsql : looping over multidimensional array : getting NULL for subdimension

Hi all,

I'm trying to loop over a multidimensional array and find if any of the elements in a sub-dimensional array are matching some known criteria but facing issue with NULL for the sub arrays.

I have a data structure that looks like:

   some_array VARCHAR[][] := '{{samba,sarath,sreenivas},{samba,mukhesh,pavan}}';

I'm trying to assign the individual sub arrays to other array elements like:

  other-array VARCHAR[];

  other_array=some_array[1];


and I'm expecting to get '{samba,sarath,sreenivas}' for index 1 and {samba,mukhesh,pavan} for index 2;
however, I'm getting NULL.

Can some one explain the way I can assign subarrays to other array elements plpgsql?

Thanks and Regards,
Samba

PS;
Sorry for spamming but my earlier mail was having some issue with the Subject line because of  resending stalled post.
On Wed, Dec 1, 2010 at 10:51 AM, Samba <saasira@gmail.com> wrote:
> Hi all,
>
> I'm trying to loop over a multidimensional array and find if any of the
> elements in a sub-dimensional array are matching some known criteria but
> facing issue with NULL for the sub arrays.
>
> I have a data structure that looks like:
>
>    some_array VARCHAR[][] :=
> '{{samba,sarath,sreenivas},{samba,mukhesh,pavan}}';
>
> I'm trying to assign the individual sub arrays to other array elements like:
>
>   other-array VARCHAR[];
>
>   other_array=some_array[1];
>
> and I'm expecting to get '{samba,sarath,sreenivas}' for index 1 and
> {samba,mukhesh,pavan} for index 2;
> however, I'm getting NULL.
>
> Can some one explain the way I can assign subarrays to other array elements
> plpgsql?

There is no truly effective way to do that.  you can however slice the
array which is almost as good:
other_array=some_array[1:1];
note this will give {{samba,sarath,sreenivas}}, not {samba,sarath,sreenivas}

merlin

Thank you  Merlin for your answer,

       I tried that and is working as you said.
       But when I do
                   other_array=some_array[1:2];
       I'm getting the entire '{{samba,sarath,sreenivas},{samba,mukhesh,pavan}}';

       not just the second subarray, i.e. '{{samba,mukhesh,pavan}}'.

       Is there any way I can get only the corresponding slice of the array?

       otherwise I need to call this function multiple times from my java client, once per each sub-array which I think will more expensive
       that doing it in just one stored procedure call.

Regards,
Samba

   

On Thu, Dec 2, 2010 at 2:04 AM, Merlin Moncure <mmoncure@gmail.com> wrote:
On Wed, Dec 1, 2010 at 10:51 AM, Samba <saasira@gmail.com> wrote:
> Hi all,
>
> I'm trying to loop over a multidimensional array and find if any of the
> elements in a sub-dimensional array are matching some known criteria but
> facing issue with NULL for the sub arrays.
>
> I have a data structure that looks like:
>
>    some_array VARCHAR[][] :=
> '{{samba,sarath,sreenivas},{samba,mukhesh,pavan}}';
>
> I'm trying to assign the individual sub arrays to other array elements like:
>
>   other-array VARCHAR[];
>
>   other_array=some_array[1];
>
> and I'm expecting to get '{samba,sarath,sreenivas}' for index 1 and
> {samba,mukhesh,pavan} for index 2;
> however, I'm getting NULL.
>
> Can some one explain the way I can assign subarrays to other array elements
> plpgsql?

There is no truly effective way to do that.  you can however slice the
array which is almost as good:
other_array=some_array[1:1];
note this will give {{samba,sarath,sreenivas}}, not {samba,sarath,sreenivas}

merlin

On Thu, Dec 2, 2010 at 9:03 AM, Samba <saasira@gmail.com> wrote:
> Thank you  Merlin for your answer,
>
>        I tried that and is working as you said.
>        But when I do
>                    other_array=some_array[1:2];
>        I'm getting the entire
> '{{samba,sarath,sreenivas},{samba,mukhesh,pavan}}';
>
>        not just the second subarray, i.e. '{{samba,mukhesh,pavan}}'.
>
>        Is there any way I can get only the corresponding slice of the array?

sure...some_array[2:2], etc

merlin

On Thu, Dec 2, 2010 at 12:20 PM, Merlin Moncure <mmoncure@gmail.com> wrote:
> On Thu, Dec 2, 2010 at 9:03 AM, Samba <saasira@gmail.com> wrote:
>> Thank you  Merlin for your answer,
>>
>>        I tried that and is working as you said.
>>        But when I do
>>                    other_array=some_array[1:2];
>>        I'm getting the entire
>> '{{samba,sarath,sreenivas},{samba,mukhesh,pavan}}';
>>
>>        not just the second subarray, i.e. '{{samba,mukhesh,pavan}}'.
>>
>>        Is there any way I can get only the corresponding slice of the array?
>
> sure...some_array[2:2], etc

by the way, see:
http://archives.postgresql.org/pgsql-general/2010-04/msg01041.php (and
responses) for a more in depth treatment of the issue.

merlin