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.
sub get_links {
my @links;
open(my $fh, "<", "links.alpha.sorted.25sample")
or die "cannot open < links.alpha.sorted.25sample: $!";
while(<$fh>) {
chomp;
my $tuple;
@$tuple = split(/\s+/, $_);
push(@links, $tuple);
}
return @links;
}
Then I sort by the second value and write it to another file:
sub sort_and_store {
my @links = get_links();
my @sorted_links = sort { $a->[1] cmp $b->[1] } @links;
open(my $fh, ">", "sorted.by.destination")
or die "cannot open > sorted.by.destination: $!";
foreach my $tuple (@sorted_links) {
print $fh "@$tuple\n";
}
}
I've never used the @$blah 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? |