in reply to custom sorting

For instance, with this:

print for map { substr $_, 30 } sort map { sprintf "%-30s%s", (split / /, (split /\|/)[1])[1], $_ } <DATA>; __DATA__ 123|Pete Smith|4321|321G4 2134|Mike Malarky|93F821 83|Dave Waylaid|8374W

Output:

2134|Mike Malarky|93F821 123|Pete Smith|4321|321G4 83|Dave Waylaid|8374W

The difficult part is that nested split. The inner one, (split /\|/)[1] selects the second field in your data. Then the result is fed to the outer, (split / /, inner)[1], which extracts the second word. That is mapped to a string like this: Malarky                       2134|Mike Malarky|93F821. This string is ASCIIbetically sorted and then, the right part of it is extracted with substr and returned to print. This technique (mapping to a string, sorting and extracting the previously mangled data) is called Guttman-Rosler transform, google for it.

As a side note, I'd like to point out that the first line in your example data appears to have four fields separated with a pipe, while the rest have only three fields.

--
David Serrano

Replies are listed 'Best First'.
Re^2: custom sorting
by Anonymous Monk on Jul 04, 2008 at 14:41 UTC
    That's a double-s in Rossler.

      According to google:

      • "gutman-rosler": 1 match.
      • "gutman-rossler": 3 matches.
      • "guttman-rosler": 1660 matches.
      • "guttman-rossler": 5 matches.

      --
      David Serrano