Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

ordering array of sentences

by welle (Beadle)
on Nov 13, 2012 at 16:00 UTC ( [id://1003644]=perlquestion: print w/replies, xml ) Need Help??

welle has asked for the wisdom of the Perl Monks concerning the following question:

I have an array (@sentences) containing sentences, for example

I am searching for a word What are you searching for? Today I have been searching for a new job

I would like to sort them alphabetically according to the word on the left side of the word @query="searching". The sorted order would be:

I am searching for a word Today I have been searching for a new job What are you searching for?

Any ideas how to get started? I am really stuck... I guess, I should use split (\s), maybe map or index, and of course cmp. But I have really no idea how to start...So, any idea would be appreciated

Replies are listed 'Best First'.
Re: ordering array of sentences
by choroba (Cardinal) on Nov 13, 2012 at 16:15 UTC
    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;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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.

      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.

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

      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: ordering array of sentences
by kennethk (Abbot) on Nov 13, 2012 at 16:11 UTC
    This definitely sounds like a job for regular expressions. You should be able to grab the word to the left of searching Using character classes, specifically \S, which "represents any non-whitespace character". You can then use that to write your own sorting routine, as described in sort. Don't forget that cmp is case sensitive, so you may want to use lc in your comparison.

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

Re: ordering array of sentences
by Anonymous Monk on Nov 13, 2012 at 16:08 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1003644]
Approved by Tanktalus
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 17:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found