Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^3: ordering array of sentences

by choroba (Chancellor)
on Nov 13, 2012 at 16:58 UTC ( #1003665=note: print w/replies, xml ) Need Help??

in reply to Re^2: ordering array of sentences
in thread ordering array of sentences

As usual in functional programming, read from right to left (or bottom to top):

The map changes the sentences to arrays of arrays. Each such an array has two elements: the word to sort on and the original sentence.

sort just sorts the arrays according to their first element, i.e. the sort word.

map transforms the arrays back to sentences.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^4: ordering array of sentences
by welle (Beadle) on Nov 13, 2012 at 17:51 UTC

    I got it. Thank you!

    In a very ugly Perl, I translated it so:

    my @sentences1 = map { /(\w+)\s+searching/; [$1, $_] } @sentences +; my @sentences2 = sort { $a->[0] cmp $b->[0] } @sentences1; my @sentences3 = map {$_->[1]} @sentences2; foreach (@sentences3) { print $_; }

    ...just to lear Perl

Re^4: ordering array of sentences
by welle (Beadle) on Nov 13, 2012 at 19:24 UTC

    What if my @sentences has a more complicated structure? I get @sentences from querying a Sqlite database with the following:

    $sentences = $dbh->selectall_arrayref("SELECT ID, sentence FROM texts +WHERE sentence REGEXP '(?i:$query)'");

    As one can see, the result is made of two elements (ID and sentence). How can I read/manipulate in the code provided by choroba only the second element "sentence", preserving at the end of the sorting operation the same structure ID/sentence?

      1. use ORDER BY in SQL to have the data delivered to perl in the expected order
      2. Schwartzian transform


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003665]
[ambrus]: I really don't like automagic stuff. I'm happy when computers do exactly what I tell them, even if that means they sometimes do the wrong thing.
[ambrus]: And I don't much like syntax highlighters. If you need a syntax highlighter to understand your code, then your code is written unclear.
[ambrus]: And if you need a syntax highlighter to color parenthesis green and numbers black and letters blue, then you're using the wrong font.
[ambrus]: I have to tolerate syntax highlighters when other people use them, but I don't use them myself. And sorry for the rant.
[GotToBTru]: I appreciate the ability to highlight matching brackets/ parentheses/ braces, both for my own code and the inconsistently indented code of others
[Corion]: I like editors that automatically highlight the matching parenthesis (like % in vi), but that's roughly the extent to which I like editor support ;)
[GotToBTru]: same here Corion.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2017-02-27 12:47 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.