
rGeoffrey
User
/ Moderator
Mar 2, 2001, 7:10 PM
Post #8 of 8
(1644 views)
|
Consider an input file that looks like this
george,gjetson@spacely.com,www.spacely.com fred,fflintstone@slate.com,www.bedrock.org homer,hsimpson@burns.com,www.donuts.com Now we will read from bottom to top and assume that $SortField == 2 First we read from the file. After " <INPUT> " the array looks like...
@array = ( 'george,gjetson@spacely.com,www.spacely.com', 'fred,fflintstone@slate.com,www.bedrock.org', 'homer,hsimpson@burns.com,www.donuts.com', ); Then we build an array of arrays where the field we want to sort is in position 0 and the whole string is in position 1. After " map { [(split (',', $_))[$SortField], $_] } " the array looks like...
@array = ( ['www.spacely.com', 'george,gjetson@spacely.com,www.spacely.com'], ['www.bedrock.org', 'fred,fflintstone@slate.com,www.bedrock.org'], ['www.donuts.com', 'homer,hsimpson@burns.com,www.donuts.com'], ); Now we can sort on the first field to get the array of arrays in the right order. After " sort { $a->[0] cmp $b->[0] } " the array looks like...
@array = ( ['www.bedrock.org', 'fred,fflintstone@slate.com,www.bedrock.org'], ['www.donuts.com', 'homer,hsimpson@burns.com,www.donuts.com'], ['www.spacely.com', 'george,gjetson@spacely.com,www.spacely.com'], ); Then we retrieve just the part we care about. After " map { $_->[1] } " the array looks like...
@array = ( 'fred,fflintstone@slate.com,www.bedrock.org', 'homer,hsimpson@burns.com,www.donuts.com', 'george,gjetson@spacely.com,www.spacely.com', ); And that is the array that is printed to OUTPUT. PS. As I did not use chomp, the newline ("\n") is still at the end of each line and both elements of the arrays. But I left them out of the example to make things cleaner. -- Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?
|