我正在尝试拆分多个连接的单词,我有一个我从中抓取的perl脚本 How can I split multiple joined words?
脚本输出多个选项,但我只需要最后一个,通常是正确的,我应该在脚本中更改什么才能实现此目的?
#!/usr/bin/perl
use strict;
my $WORD_FILE = 'dic_master'; #Change as needed
my %words; # Hash of words in dictionary
# Open dictionary, load words into hash
open(WORDS, $WORD_FILE) or die "Failed to open dictionary: $!\n";
while (<WORDS>) {
chomp;
$words{lc($_)} = 1;
}
close(WORDS);
# Read one line at a time from stdin, break into words
while (<>) {
chomp;
my @words;
find_words(lc($_));
}
sub find_words {
# Print every way $string can be parsed into whole words
my $string = shift;
my @words = @_;
my $length = length $string;
foreach my $i ( 1 .. $length ) {
my $word = substr $string, 0, $i;
my $remainder = substr $string, $i, $length - $i;
# Some dictionaries contain each letter as a word
next if ($i == 1 && ($word ne "a" && $word ne "i"));
if (defined($words{$word})) {
push @words, $word;
if ($remainder eq "") {
print join(' ', @words), "\n";
return;
} else {
find_words($remainder, @words);
}
pop @words;
}
}
return;
}
谢谢!
答案 0 :(得分:4)
只需将print
中的find_words
替换为变量赋值,然后在for
循环结束后将其打印出来。
答案 1 :(得分:1)
bvr's answer将解决问题的迫切需要。
建议使用exists
代替defined
来检查字符串中是否存在字符串。这将确保诸如'bemyg'
之类的非单词永远不会成为字典哈希中的键。