in reply to
Re^2: Sorting a "tuple" by the second value
in thread Sorting a "tuple" by the second value
You could combine the steps in your get_links() and sort_and_store() subroutines into one process.
knoppix@Microknoppix:~$ cat spw964070.in
abc peter
def jack
ghi zak
jkl ben
mno mick
pqr alan
knoppix@Microknoppix:~$ perl -Mstrict -wE '
> open my $inFH, q{<}, q{spw964070.in}
> or die qq{open: < spw964070.in: $!\n};
> open my $outFH, q{>}, q{spw964070.out}
> or die qq{open: > spw964070.out: $!\n};
>
> print $outFH
> map { $_->[ 0 ] }
> sort { $a->[ 2 ] cmp $b->[ 2 ] }
> map { [ $_, split ] }
> <$inFH>;
>
> close $inFH
> or die qq{close: < spw964070.in: $!\n};
> close $outFH
> or die qq{close: > spw964070.out: $!\n};'
knoppix@Microknoppix:~$ cat spw964070.out
pqr alan
jkl ben
def jack
mno mick
abc peter
ghi zak
knoppix@Microknoppix:~$
Sticking to your subroutines, it would be more efficient for get_links() to return a reference to @info than the huge array itself, like so:-
...
return \ @links;
}
sub sort_and_store {
my $refToLinks = get_links();
my @sorted_links = sort { $a->[1] cmp $b->[1] } @$refToLinks;
...
I hope these points are helpful.