<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi Jungs,<div><br></div><div>mag hier niemand das "Schwarzian Transform"?</div><div><br></div><div><font class="Apple-style-span" face="'Courier New'"><div>#!/usr/bin/perl</div><div><br></div><div>use strict;</div><div>use warnings;</div><div><br></div><div>my @liste = ( { x => 1, y => 4 }, { x => 3, y => 2 } );</div><div><br></div><div>my $key = 'y';</div><div><br></div><div>my @sortierte_liste = map  { $_->[0]             }<span class="Apple-tab-span" style="white-space:pre">   </span># nur noch das original ref weiter geben</div><div>                      sort { $a->[1] <=> $b->[1] }<span class="Apple-tab-span" style="white-space:pre">        </span># billige Vergleich</div><div>                      map  { [ $_, $_->{$key} ]  }<span class="Apple-tab-span" style="white-space:pre">    </span># berechne den teueren Sort-Schlüssel</div><div>                      @liste;</div><div><br></div><div>foreach my $h ( @sortierte_liste ) {</div><div>    printf "%d %d\n", $h->{x}, $h->{y};</div><div>}</div><div><br></div></font><div>(das geht natürlich auch ohne zwischen-Liste)</div></div><div>Vorteile:</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>beliebig komplexe Sortierungen werden nicht beliebig komplex (die Struktur bleibt immer gleich)</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>wenn teuerer Vergleichswerte im Spiel sind - werden sie nur einmal pro Eintrag in der Liste berechnet (ie: wichtig wenn man Objekte sortieren will)</div><div><br></div><div>Nur meine 3,56€ (2c + Inflation :-()</div><div><br></div><div>Steve</div><div><br></div><div><div><div>On 19.11.2012, at 12:40, Harald Joerg wrote:</div><br><blockquote type="cite"><div>Hallo Robert,<br><br>Dass Deine Überlegungen nicht funktionieren, liegt daran, dass<br>sort SUBNAME LIST syntaktisch etwas "eingeschränkt" ist.<br><br>Es geht aber, mit einfacherem bykey, wie folgt:<br><br>----------------------------------------------------------------------<br>#!/usr/bin/perl -w<br><br>use strict;<br><br>my @liste = ({ x => 1, y => 4}, {x => 3, y => 2});<br><br>foreach my $h (sort {bykey('x')} @liste){<br>     print $h->{x}, $h->{y}, "\n";<br>}<br><br>sub bykey {<br>     my $key = shift;<br><br>     return($a->{$key} <=> $b->{$key});<br>}<br>----------------------------------------------------------------------<br><br>   * Anstelle von sort SUBNAME LIST verwende ich sort BLOCK LIST,<br>     das frisst der Parser leichter.<br><br>   * In bykey sind $a und $b direkt verfügbar, das sind package globals<br>     (@Marek: Die muss man also nicht übergeben). Der Code ist nicht<br>     reentrant, es wäre also unklug, innerhalb von bykey nochmal einen<br>     sort-Aufruf zu haben (ginge auch, aber nur über sort mit Prototypes).<br><br>     Es ist aber nicht nötig, eine eigene Subroutine zu erzeugen.<br>     (@Roderich: Geht nicht - gibt's nicht :))<br>-- <br>Cheers,<br>haj<br><br><br><br>"Robert C. Helling" <<a href="mailto:helling@lmu.de">helling@lmu.de</a>> writes:<br><br><blockquote type="cite">Hallo,<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">ich stehe grade mal wieder auf dem Schlauch. Ich habe eine Liste mit <br></blockquote><blockquote type="cite">Hashreferenzen, wobei die Hashes mehrere Felder vom gleichen Typ haben. <br></blockquote><blockquote type="cite">Nun moechte ich gerne die Liste sortieren anhand jeweils eines Feldes des <br></blockquote><blockquote type="cite">Hashes. Die Vergleichsfunktion ist etwas kompliziert. Daher will ich sie <br></blockquote><blockquote type="cite">nicht fuer jedes Hashfeld neu schreiben, sondern mach lieber eine <br></blockquote><blockquote type="cite">gemeinsame Vergleichsfunktion, die den Namen des Feldes bekommt und dann <br></blockquote><blockquote type="cite">entsprechend gecurried wird. Schematisch wie in diesem Beispiel:<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">#!/usr/bin/perl -w<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">use strict;<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">my @liste = ({ x => 1, y => 4}, {x => 3, y => 2});<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">my $byx = &bykey('x');<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">foreach my $h (sort $byx @liste){<br></blockquote><blockquote type="cite">     print $h->{x}, $h->{y}, "\n";<br></blockquote><blockquote type="cite">}<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">sub bykey {<br></blockquote><blockquote type="cite">     my $key = shift;<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">     return(sub {$a->{$key} <=> $b->{$key}});<br></blockquote><blockquote type="cite">}<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Das funktioniert so. Meine Frage ist nun: Wie komme ich ohne die <br></blockquote><blockquote type="cite">Dummyvariable $byx aus? Ich habe zB schon probiert<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">foreach my $h (sort &&bykey('x') @liste){<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">aber das gibt einen Syntaxerror. Auch aehnliche Versionen mit weniger &'s <br></blockquote><blockquote type="cite">und mehr oder wenigen Klammern verschiedenen Typs habe ich probiert, aber <br></blockquote><blockquote type="cite">nichts davon funktionierte.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Wer erleuchtet mich?<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Viele Gruesse<br></blockquote><blockquote type="cite">Robert<br></blockquote>_______________________________________________<br>Munich-pm mailing list <a href="http://munich.pm.org/">http://munich.pm.org/</a><br><a href="mailto:Munich-pm@pm.org">Munich-pm@pm.org</a><br><a href="http://mail.pm.org/mailman/listinfo/munich-pm">http://mail.pm.org/mailman/listinfo/munich-pm</a><br></div></blockquote></div><br></div></body></html>