Patrick Salmon wrote: > > I've been working on sorting by IP Address in an output file that comprises > field1(hostname), field2(hosttype), IP Address. > > Found a very elegant solution here (http://www.perlmonks.org/index.pl?node_id=88719), > that I think I can re-write to suite my needs but am having a tough time > figuring out exactly what's been done. Some assistance on guiding me as to > what it's doing would be greatly appreciated - don't want someone to tell me > how to re-write, please, just want to fully understanding how a clearly > well-thought out piece of code really works so that I can re-apply it to suit. > > This is for sorting IP1, IP2 and a text field. Here's the code along with my > comments as to what I think is going on > > print > map{sprintf(("%d.%d.%d.%d,"x2)."%s\n",(split/,/)[1..8,0])} # Break the IP > addresses into numeric strings. Do it 2 times. > # Is the '"%s\n"' identifying the final (text) field? Then it'll 'split' > the record on each delimiting comma. > sort > map{sprintf"%s".",%3d"x8,(split/\W/)[8,0..7]} > # Put it back together in the same field sequence? Don't quite see how the > "USA" entry is being put back into the string. > # Why 'split', and not 'join'? > <DATA> > 1.2.3.4,1.2.3.255,USA > 2.3.4.0,2.3.4.25,USA > > What's really causing my head to ache is not fully understanding what the > [1..8,0] and [8,0..7] values are doing and their effect upon the resulting > string. Does the '8' value reflect the total number of IP fields to be > worked on? What's happening with the final string of text? > > I think I'm _real_ close to fully understanding this (in fact, writing out > this mail has helped a chunk, too, by making me think through exactly what > I'm asking for help with), but am that teeny little bit short of an epiphany > here.
The algorithm used is the Guttman-Rosler Transform which is based on the Schwartzian Transform. The paper describing it can be found here: http://www.sysarch.com/perl/sort_paper.html A better way to convert IP addresses for sorting is to use pack() and unpack() or the inet_ntoa() and inet_aton() functions from the Socket module. (EXRESSION)[1..8,0] is a list slice where the expression returns a list but you only want the first nine values in the order 1, 2, 3, 4, 5, 6, 7, 8, 0. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]