: : : Compliments Everyone!!
: : :
: : : How do I delete repetition words in a file.If my file is like this:
: : :
: : : ABE
: : : ABE
: : : TWO
: : : TWO
: : : TWO
: : : IT
: : : IT
: : : IT
: : : IT
: : :
: : : So I want it to have only:
: : : ABE
: : : TWO
: : : IT
: : Put the words in an array, one word in each element. Then use a hash to keep track of words you've seen and use grep to only copy those you haven't. For example:-
: :
: : my %temp = ();
: : @uniques = grep { $temp{$_}++ == 0 } @allwords;
: :
: : Jonathan
:
: Correct me if I'm wrong but the way Jonathan has it, it will only
: select the words that have repititions. The way I understand it is
: to have every word only show up once in the file, even if the word
: is used once.
It will show up if only used once. Remember that the ++ operator casues evaluation before incrementation. If you evaluate a (currently) non-existent hash value in numeric context you get 0, 0 == 0 is true so the item is copied into @uniques. If it's seen again, we hash value for that word exists and is 1. 1 == 0 is false, so it's not included in the list again.
A nice side effect of this method is that you get a hash that tells you how many times each word appears as a bonus.
I didn't test it before posting because I was pretty certain it was right - I have tested it with this script:-
my @allwords = qw(it it and it be poo and hey);
my %temp = ();
@uniques = grep { $temp{$_}++ == 0 } @allwords;
print "$_\n" for (@uniques);
And when I run that I get:-
it
and
be
poo
hey
: If this is the case, then you can take out the grep part of that
: code and have:
: @uniques = { $temp{$_}++ == 0 } @allwords;
: I think that will work. If not, then I know someone will be able to
: tell you right. If what Jonathan gave you is what you are looking
: for, then I guess you have another way to look at it.
Tried that out of curiosity (didn't think it looked right)...and...
Array found where operator expected at test.pl line 4, near "} "
(Missing operator before ?)
syntax error at test.pl line 4, near "} @allwords"
Execution of test.pl aborted due to compilation errors.
I *think* what I wrote does solve the problem - but I may still be missing something obvious.
Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");