Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Sorting a "tuple" by the second value

by johngg (Abbot)
on Apr 09, 2012 at 12:22 UTC ( #964115=note: print w/ replies, xml ) Need Help??


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.

Cheers,

JohnGG


Comment on Re^3: Sorting a "tuple" by the second value
Select or Download Code
Re^4: Sorting a "tuple" by the second value
by thmsdrew (Scribe) on Apr 09, 2012 at 18:03 UTC

    Definitely helpful. I need to learn more about referencing and when it's best to use. Thanks!

      “Referencing” is a drop-dead simple idea, actually:   a reference is “a tiny thing” that refers to something else in memory.   That thing which is referred-to may be big or small, and there may be just one reference to it or a great many.   But each reference is very small and cheap.   So, for example, if you have in-memory a list of “big records,” you can swap and manipulate and what-have-you those records just by playing around with lists (or arrays, or hashes) of references to them:   nothing actually “moves.”   This lets you build up arbitrarily-complex data structures very easily, and to work with them very efficiently.   (Want to sort the same list of records by three separate keys at once, all in-memory, keeping all three lists, without making duplicate copies of anything?   Feel Free.™)   It’s one of the hallmarks of Perl and one of the compelling reasons for using this language.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://964115]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-09-21 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (173 votes), past polls