Thread: sort array optimisation in pl/perl

sort array optimisation in pl/perl

From
"GIROIRE Nicolas (COFRAMI)"
Date:

Hi,

I create an array which is result of query on postgresql database and then I want to sort rows in a particular way (impossible by query on database).

My solution consists to put a rows (indice m+1) in a temporary other and then move all element before indice n to m in rows with indice n+1 to m+1 and last i put my temporary variable to indice n.

I want to know if somebody know a better solution.

I think of 2 solutions but i don't success to apply :
  - the first is to use list in which I could deplace references as a chained list
  - the second will be to deplace tab[n..m] to tab[n+1..m+1] in one instruction as ada language

Is one of this solution exists and is better than my first ? If yes, can you help me to implement ?

Best regards,

Nicolas

Re: sort array optimisation in pl/perl

From
Ragnar Hafstað
Date:
On Thu, 2005-03-24 at 15:49 +0100, GIROIRE Nicolas (COFRAMI) wrote:

> I create an array which is result of query on postgresql database and
> then I want to sort rows in a particular way (impossible by query on
> database).

can you give us more details on this particular sort order?

> My solution consists to put a rows (indice m+1) in a temporary other
> and then move all element before indice n to m in rows with indice n+1
> to m+1 and last i put my temporary variable to indice n.
> I want to know if somebody know a better solution.
>
> I think of 2 solutions but i don't success to apply :
>   - the first is to use list in which I could deplace references as a
> chained list
>   - the second will be to deplace tab[n..m] to tab[n+1..m+1] in one
> instruction as ada language

it all depends on the expected sizes of your arrays, but perl has
some nice array operations, such as slices and splice()

these come to mind:

$x=$arr[$m+1];@arr[$n+1..$m+1]=@arr[$n..$m];$arr[$n]=$x;

or:

@arr[$n..$m+1]=($arr[$m+1],@arr[$n..$m]);

gnari



Re: sort array optimisation in pl/perl

From
"GIROIRE Nicolas (COFRAMI)"
Date:

Hi,

thanks for this help.

The method functions great but the profit of time is good just if I have a lot of elements to deplace.

        COFRAMI
        Nicolas Giroire
        on behalf of AIRBUS France
        for In Flight & Ground Information Services - Development
        Phone : +33 (0)5 67 19 98 74
              Mailto:nicolas.giroire@airbus.com

-----Message d'origine-----
De : pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org]De la part de Ragnar Hafstað
Envoyé : jeudi 24 mars 2005 21:53
À : GIROIRE Nicolas (COFRAMI)
Cc : 'pgsql-general@postgresql.org'
Objet : Re: [GENERAL] sort array optimisation in pl/perl

On Thu, 2005-03-24 at 15:49 +0100, GIROIRE Nicolas (COFRAMI) wrote:

> I create an array which is result of query on postgresql database and
> then I want to sort rows in a particular way (impossible by query on
> database).

can you give us more details on this particular sort order?

> My solution consists to put a rows (indice m+1) in a temporary other
> and then move all element before indice n to m in rows with indice n+1
> to m+1 and last i put my temporary variable to indice n.
> I want to know if somebody know a better solution.
>
> I think of 2 solutions but i don't success to apply :
>   - the first is to use list in which I could deplace references as a
> chained list
>   - the second will be to deplace tab[n..m] to tab[n+1..m+1] in one
> instruction as ada language

it all depends on the expected sizes of your arrays, but perl has
some nice array operations, such as slices and splice()

these come to mind:

$x=$arr[$m+1];@arr[$n+1..$m+1]=@arr[$n..$m];$arr[$n]=$x;

or:

@arr[$n..$m+1]=($arr[$m+1],@arr[$n..$m]);

gnari

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faq

This mail has originated outside your organization,
either from an external partner or the Global Internet.
Keep this in mind if you answer this message.

Re: sort array optimisation in pl/perl

From
Ragnar Hafstað
Date:
On Fri, 2005-03-25 at 15:29 +0100, GIROIRE Nicolas (COFRAMI) wrote:
[re-arranged]

> [mailto:pgsql-general-owner@postgresql.org]De la part de Ragnar
Hafstað

> > On Thu, 2005-03-24 at 15:49 +0100, GIROIRE Nicolas (COFRAMI) wrote:
>
> > > I create an array which is result of query on postgresql database
> and
> > > then I want to sort rows in a particular way (impossible by query
> on
> > > database).
>
> > [snip suggestions]
> thanks for this help.
>
> The method functions great but the profit of time is good just if I
> have a lot of elements to deplace.

in that case, it most likely need to look at your algorythm. There is
little we can help you with, asy ou have not given us any info on
your sort requirements.

gnari