### Re: Understanding transformation sorts (ST, GRT), the details

by jdporter (Canon)
 on May 18, 2005 at 13:30 UTC ( #458196=note: print w/replies, xml ) Need Help??

I agree with merlyn that it's better to try to grok the ST before the GRT, which, after all, is a special case of the ST. I'm not sure that the ST is always clearer, however.

Interesting that the example of sorting IP addresses is used here, because that was the original scenario in which the GRT was first explored (by Michal Rutka, Uri Guttman, Larry Rosler and myself). We could probably say that it is a canonical example: The data itself is very simple, the string representation cannot be (usefully) used for sorting, and the transformation to and from a meaningfully sortable representation is trivial.

```@ips =
# note: this converts any "unusual" reps to dotted-quad
map { inet_ntoa \$_ }
sort
map { inet_aton \$_ }
@ips;
Or, to sort lines which have IPs embedded:
```@ips =
map { substr \$_, 4 }
sort
map { /(\d[.\d]+\d)/; inet_aton(\$1) . \$_ }
@ips;

As you can see, this is no less clear than the equivalent ST. The maps are arguably simpler; and of course the sort is more trivial, since that's always the case.

But sorting IPs is something of a special case. In general, one would probably have to resort to either pack or sprintf to generate the sortable string.

Replies are listed 'Best First'.
Re^2: Understanding transformation sorts (ST, GRT), the details
by merlyn (Sage) on May 18, 2005 at 14:08 UTC
```map { /(\d[.\d]+\d)/; inet_aton(\$1) . \$_ }
Oops! That duplicates a value twice if the next line doesn't have a good match. Perhaps you meant:
```map { /(\d[.\d]+\d)/ ? inet_aton(\$1) . \$_ : () }

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

I'm assuming well-formed input. If there's non-conformant data in the input, it would be better to have already detected and handled it.

Create A New User
Node Status?
node history
Node Type: note [id://458196]
help
Chatterbox?
NodeReaper wonders through a maze of twisty little sayings, all alike

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-05-24 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (179 votes). Check out past polls.

Notices?