Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: sort array

by Cristoforo (Curate)
on Apr 30, 2012 at 19:26 UTC ( #968134=note: print w/replies, xml ) Need Help??

in reply to sort array

Your subroutine may work with a few changes. However, I rewrote it to try a simpler approach. Here is a sample program you can run.

#!/usr/bin/perl use strict; use warnings; my @lines = split /\n/, <<EOF; I need your help. I have an array containing sentences. I want to order the sentences according to a given word I am a nice boy You are a nice boy They are not good at all EOF print "$_\n" for order_line(@lines); sub order_line { return sort by_3d_word_from_right @_; } sub by_3d_word_from_right { { lc((split ' ', $a)[-3]) cmp lc((split ' ', $b)[-3]) } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968134]
[choroba]: We had a meeting yesterday
[choroba]: we broke the record number of participants
[choroba]: even if I don't count the two scala people who just joined a colleague
[erix]: one hopes the beer flowed copiously
[choroba]: indeed

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-09-22 13:23 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (263 votes). Check out past polls.