Re: Duplicate Words
by Anonymous Monk on Apr 20, 2001 at 02:17 UTC
|
s/(homework\s*){2}/homework /; | [reply] [d/l] |
|
|
s/(\b\w+?\b)\s+\1/$1/g;
This will match "This is a sentence sentence." as well as "This is a sentence sentence to.".
bent | [reply] [d/l] |
|
|
If you want to remove dup words from anywhere in the string rather than just consecutive duplicates, try:
s/\w+\s*/$words{$&}++?'':$&/ge;
or to ignore case:
s/\w+\s*/$words{"\L$&"}++?'':$&/ge;
| [reply] [d/l] [select] |
homework! (Re: Duplicate Words)
by tye (Sage) on Apr 20, 2001 at 02:17 UTC
|
| [reply] |
Re: Duplicate Words
by jbert (Priest) on Apr 20, 2001 at 17:13 UTC
|
OK. It is homework, but some general comments on coding:
Good luck with the coding.
PS. Where I refer to 'perlvar', 'perlre' etc, these are some of the standard documentation which comes with perl. On Windows with Activestate perl, you can often find this in HTML format on the Start button/Programs/Activeperl, and on all systems you can type "perldoc perlvar" (or whatever) at a command prompt and get the information.
| [reply] [d/l] |
Re: Duplicate Words
by orkysoft (Friar) on Apr 20, 2001 at 03:18 UTC
|
while (<@Lines>) {
Cool, I didn't know you could do it that way as well. Still I think foreach is a lot less confusing.
| [reply] [d/l] |
|
|
Would not foreach load all the lines into memory first?
| [reply] [d/l] |
|
|
They're already in memory. (I'm pretty sure)
| [reply] |
Re: Duplicate Words
by Anonymous Monk on Apr 20, 2001 at 17:08 UTC
|
How about changing the it to finish like this:
@Lines = split /\W+/, $Contents;
While (<@Lines>)
{
$Contents ~= s/$_//;
}
and save the $Contents as your returning string.
This is my guess, I'm totally new to Perl.
2001-04-20 Edit by Corion : Added CODE tags | [reply] [d/l] |
Re: Duplicate Words
by Anonymous Monk on Apr 20, 2001 at 22:31 UTC
|
The main thing that will help you here is to realize that you can use parentheses around the part of the regular expression that matches any word, and then use "\1" in the same regular expression to test for the immediate second occurrence of that word.
Actual code of course is the assignment. If you tell us when it's due, maybe we'll post solutions (probably pithier than teacher's :-) afterwards. | [reply] |
Re: Duplicate Words
by Anonymous Monk on Apr 20, 2001 at 14:06 UTC
|
#!/usr/local/bin/perl
$var="word1 word2 word word word3 someth word3";
print $var."\n";
@words=split(/ /,$var); $len=@words; $var="";
for ($i=0; $i<=$len-1; $i++)
{
if ($words[$i] eq $words[$i+1])
{print $words[$i]," detected\n"}
else
{$var.=@words[$i]." "}
}
print $var."\n";
| [reply] [d/l] |
Re: Duplicate Words
by chorg (Monk) on Apr 20, 2001 at 18:40 UTC
|
I believe that the new Camel has this problem as one of the examples - either chapter 2 or the chapter 5...
_______________________________________________
"Intelligence is a tool used achieve goals, however goals are not always chosen
wisely..." | [reply] |
Re: Duplicate Words
by Sprad (Hermit) on Apr 20, 2001 at 19:53 UTC
|
| [reply] |
|
|
For example :-
Had had had "had had", had Had had had "had" Had would have been corre
+ct.
The above was a comment about the grammar of an essay
written by an author named Had.
My memory concerns me - but I forget why !!!
| [reply] [d/l] |
| A reply falls below the community's threshold of quality. You may see it by logging in. |