in reply to Re: Sorting a "tuple" by the second value
in thread Sorting a "tuple" by the second value
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Sorting a "tuple" by the second value
by johngg (Canon) on Apr 09, 2012 at 12:22 UTC | |
by thmsdrew (Scribe) on Apr 09, 2012 at 18:03 UTC | |
by locked_user sundialsvc4 (Abbot) on Apr 09, 2012 at 18:38 UTC | |
|
Re^3: Sorting a "tuple" by the second value
by Anonymous Monk on Apr 09, 2012 at 08:13 UTC | |
by thmsdrew (Scribe) on Apr 09, 2012 at 18:00 UTC |