Thread Liste von Arrays sortieren (14 answers)
Opened by scriptor at 2018-04-12 08:54

Linuxer
 2018-04-12 10:03
#188261 #188261
User since
2006-01-27
3870 Artikel
HausmeisterIn

user image
Hi,

hier kann die Schwartz'sche Transformation (Schwartzian Transform) helfen.

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#! /usr/bin/perl
use strict;
use warnings;
use 5.010;

use Data::Dumper;

my @array = (
    [ 3, 7 ],
    [ 1, 2 ],
    [ 2, 4 ],
);


# Schwartzian Transform; read it from the end
my @sorted =
  # restore original data
  map { $_->[0] }
    # sort numerically by the calculated differemce
    sort { $a->[1] <=> $b->[1] }
      # create temp array ref; first element is the original data
      # second element is the square of the difference between second and first value of original dataset
      map { [ $_, ($_->[1] - $_->[0])**2 ] }
        # parse list of array duples
        @array;

say Dumper \@sorted;


edit: Quadratur eingebaut
Last edited: 2018-04-12 10:56:12 +0200 (CEST)
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Liste von Arrays sortieren