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;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: ordering array of sentences
by kennethk (Abbot) on Nov 13, 2012 at 16:26 UTC | |
Re^2: ordering array of sentences
by welle (Beadle) on Nov 13, 2012 at 16:52 UTC | |
by choroba (Cardinal) on Nov 13, 2012 at 16:58 UTC | |
by welle (Beadle) on Nov 13, 2012 at 17:51 UTC | |
by welle (Beadle) on Nov 13, 2012 at 19:24 UTC | |
by afoken (Chancellor) on Nov 14, 2012 at 06:18 UTC | |
Re^2: ordering array of sentences
by welle (Beadle) on Nov 13, 2012 at 19:27 UTC |
In Section
Seekers of Perl Wisdom