Re: How to delete few elements from array beginning?

From: Chris Travers <chris(dot)travers(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to delete few elements from array beginning?
Date: 2016-03-09 16:03:32
Message-ID: CAKt_Zfskp6fgxAq7RXJis1ugtu=bk02XZpK70-y5bFDxnqqFXg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Mar 9, 2016 at 4:53 PM, Alexander Farber <alexander(dot)farber(at)gmail(dot)com
> wrote:

> Hello Chris,
>
> On Wed, Mar 9, 2016 at 4:42 PM, Chris Travers <chris(dot)travers(at)gmail(dot)com>
> wrote:
>
>>
>>
>> On Wed, Mar 9, 2016 at 1:10 PM, Alexander Farber <
>> alexander(dot)farber(at)gmail(dot)com> wrote:
>>
>>>
>>> what is please the most efficient way to delete a slice from the start
>>> of a longer array (after I have copied it to another array)?
>>>
>>> Do I really have to copy a large slice of the array to itself, like in
>>> the last line here:
>>>
>>> pile_array := pile_array || swap_array;
>>>
>>> /* here I copy away swap_len elements */
>>> new_hand := pile_array[1:swap_len];
>>>
>>> /* here I don't know how to efficiently remove already copied
>>> elements */
>>> pile_array := pile_array[(swap_len + 1):ARRAY_LENGTH(pile_array,
>>> 1)];
>>>
>>> or is there a better way?
>>>
>>
>> This looks like something for card hands?
>>
>> What you are doing seems correct to me.
>>
>>
> actually card hands would be easier - because cards are unique in the deck.
>
> But here I have letter hands (like "AAABCDE") in a word game and they are
> not unique in the pile...
>

But it seems like a similar problem. Namely:

1. You have an ordered list of cards (these may be lettered cards). These
start out as existing in a shuffled deck but once shuffled you have an
order. Whether or not the cards are unque

2. When you draw a card, you have to remove it from the head of the list.

Now on to some basic points (though you can probably do this in a straight
SQL query):

SQL arrays are more or less like math matrices. For this reason it doesn't
make sense to treat them as programming structures per se. Instead you
have a matrix and you create another matrix by slicing it. So this is how
you have to think about the problem.

Also consider that PostgreSQL tuples are copy on write anyway.

Now, if you want something that uses an array more like a queue, it is best
to write that portion in pl/perl. However if you do that you have to think
carefully about mutability.

Hope this helps
Chris Travers

>
> Regards
> Alex
>
>

--
Best Wishes,
Chris Travers

Efficito: Hosted Accounting and ERP. Robust and Flexible. No vendor
lock-in.
http://www.efficito.com/learn_more

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Lupi Loop 2016-03-09 17:09:36 Re: Windows default directory for client certificates
Previous Message Alexander Farber 2016-03-09 15:53:54 Re: How to delete few elements from array beginning?