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

Re: ordering array of sentences

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


in reply to ordering array of sentences

If the list of sentences is longer, searching for a word before another word might slow the program down (Benchmark). Schwartzian transform to the rescue:

#!/usr/bin/perl use warnings; use strict; use feature 'say'; my @sentences = ('I am searching for a word', 'What are you searching for?', 'Today I have been searching for a new job', ); my $query = 'searching'; # Not @query, it is a scalar, not an array +. say for map $_->[1], sort { $a->[0] cmp $b->[0] } map { /(\w+)\s+searching/; [$1, $_] } @sentences;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: ordering array of sentences
Download Code
Re^2: ordering array of sentences
by kennethk (Monsignor) on Nov 13, 2012 at 16:26 UTC
    Be aware there are two potential issues here, which are contained in the sentence I'm searching. First, \w+ will not match on the apostrophe, so you'll end up sorting on m instead of I'm. If you fix that, you'll still get an incorrect result because cmp is case sensitive.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re^2: ordering array of sentences
by welle (Beadle) on Nov 13, 2012 at 16:52 UTC

    Thanks,it works (couple of changes can be necessary for taking into account case, commas, etc. BUT: as I am a newbe in perl, could you "split" (i.e. putting it in simpler blocks) for me the lines

    say for map $_->[1], sort { $a->[0] cmp $b->[0] } map { /(\w+)\s+searching/; [$1, $_] } @sentences;

    so that I better understand its functioning. For example, I am trying to put the result of the sorting again in an array, but I can't do it. That would be great!

      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.

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

        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

        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?

Re^2: ordering array of sentences
by welle (Beadle) on Nov 13, 2012 at 19:27 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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-09-02 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls