> I can't figure out what this is doing... > my @sorted = map { $_->[2] } > sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } > map { [ (split /\t/)[3,2], $_ ] } > @lines;
Hmmm... let's see. > @lines; 1. Iterate over @lines > map { [ (split /\t/)[3,2], $_ ] } 2. For each line split it by tabs. Take the 4th and 3rd field (in that order), along with the whole line, and create a 3 element anonymous array from it. > sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } 3. Iterate over the list of array refs created in step #2 and sort them. Sort them first on the numeric value of the 4th field from the original string... and if they are equal then sort based on the 3rd field of the original string. > my @sorted = map { $_->[2] } 4. Take the sorted list from step #3 and only return the 3rd element, which is the original string before splitting. In short: it sorts a list of tab delimited strings, first by the 4th field, then by the 3rd field. Does that sound right? Here were your questions... > 2. Generally to reproduce a two-key sort, you sort > by the secondary key first, then the primary key. Actually, reverse that. The <=> operator returns -1, 0, or 1 based on the comparison (zero if equal). The || operator evaluates the right side ONLY if the left side fails (ie. is zero, empty, or undefined). So in the script: $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] This code says compare item #0 first, and if equal, then compare item #1. So #0 is the primary key, and #1 is secondary. > 3. What does the "->[\d]" do? It is accessing the "\d" element of the array reference. See perlreftut for a good/breif explaination of using references. > 1. How do I do a reverse sort of column 4? Change this: $a->[0] <=> $b->[0] To this: $b->[0] <=> $a->[0] Rob -----Original Message----- From: Bryan R Harris [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 10, 2002 2:18 PM To: [EMAIL PROTECTED] Subject: Re: extra space Please forgive my ignorance, but I can't figure out what this is doing. This routine correctly sorts @lines (array of lines with tab delimited fields) by column 4. # Step 3 - assumes columns 3 and 4 contain numeric data my @sorted = map { $_->[2] } sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } map { [ (split /\t/)[3,2], $_ ] } @lines; A couple of quick questions: 1. How do I do a reverse sort of column 4? 2. Generally to reproduce a two-key sort, you sort by the secondary key first, then the primary key. This assumes that the sorting routine maintains the order for equal values. Does the perl sort routine allow for this? 3. What does the "->[\d]" do? TIA. - B -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]