
mrossi
Deleted
Aug 15, 2000, 3:58 AM
Post #2 of 4
(289 views)
|
Heh, I've been struggling with a similar problem myself this afternoon! Found some help in Perlfaq4 under Data:Arrays/How do I sort an array by (anything). Here's a working example: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> @data = qw(2;c 1;b 3;a); @idx = (); for (@data) { ($item) = /.;(.)/; push @idx, ($item); } @sorted = @data[ sort { $idx[$a] cmp $idx[$b] } 0 .. $#idx ]; print "This is a normal sort:\n", sort @data; print "\nThe same, sorted by second column:\n", @sorted; </pre><HR></BLOCKQUOTE> This will work for you, too. Just populate an array with filename-keyword pairs, delimited with (a) suitable character(s), and modify the regex on line 4 accordingly. If someone could explain how this works, I'd be happy. I can't understand how $a and $b can be used as references for a seemingly unrelated array. Another example in Perlfunc is even more confusing, where $a and $b seem to be used both as references and string variables at the same time. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> @new = sort {($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]} @old; </pre><HR></BLOCKQUOTE> Any explanations gratefully received. [This message has been edited by mrossi (edited 08-15-2000).]
|