<?xml version="1.0" encoding="windows-1252"?>
<node id="964086" title="Re^2: Sorting a &quot;tuple&quot; by the second value" created="2012-04-09 00:53:50" updated="2012-04-09 00:53:50">
<type id="11">
note</type>
<author id="964068">
thmsdrew</author>
<data>
<field name="doctext">
&lt;p&gt;Thanks for that link, it was helpful for sure. Here's how I did it. The first subroutine gets the tuples from the file, splits them into an array, and writes them all into one array.&lt;/p&gt;
&lt;code&gt;sub get_links {
	my @links;
	open(my $fh, "&lt;", "links.alpha.sorted.25sample")
		or die "cannot open &lt; links.alpha.sorted.25sample: $!";
	while(&lt;$fh&gt;) {
		chomp;
		my $tuple;
		@$tuple = split(/\s+/, $_);
		push(@links, $tuple);
	}
	return @links;
}&lt;/code&gt;
&lt;p&gt;Then I sort by the second value and write it to another file:&lt;/p&gt;
&lt;code&gt;sub sort_and_store {
	my @links = get_links();
	my @sorted_links = sort { $a-&gt;[1] cmp $b-&gt;[1] } @links;
	open(my $fh, "&gt;", "sorted.by.destination")
		or die "cannot open &gt; sorted.by.destination: $!";
	foreach my $tuple (@sorted_links) {
		print $fh "@$tuple\n";
	}
}&lt;/code&gt;
&lt;p&gt;I've never used the &lt;code&gt;@$blah&lt;/code&gt; variable type before, and I couldn't find any information about it. I'm guessing that's how you refer to the arrays in an array of arrays?&lt;/p&gt;</field>
<field name="root_node">
964070</field>
<field name="parent_node">
964077</field>
</data>
</node>
