better has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks,
I'm trying to compare two arrays. I know there are many ways to go: After trying, I ended up with use List::MoreUtils qw {any};
It works fine, in a testing environment, with two small arrays defined within the script
my @cleanwords = qw (hut hat); my @allwords = qw (hit het hat); foreach my $var (@cleanwords) { if (any { $var eq $_} @allwords) { print "Found: ",$var, "\n"; } else { print $var," not found\n"; } }
But the process doesn't work, after reading a textfile (list of words) into an array called @allwords. For reading I use:
use File::Slurp::Tiny 'read_file'; my @allwords = read_file ($ref);
The array is definetely filled with all the words of the textfile. But no matches are found.
I think the problem might be, the whole list is probably "slurped" into one element of the array only.
print scalar @allwords;reports back: '1'
But why are all words printed nicely one beneath the other doing:
foreach (@allwords){ print "allwords: ",$_,"\n"; }
Result is:
allwords: hastig
hastige
hastigem
hastigen
hastiger
hat
???
It looks almost like a display of sperate elements. Alomost, because 'allwords:'should be printed in front of every word.
Well, I think I have to learn more about creating arrays from a file.
It's a special challenge, regarding the functionality of the script, because @allwords should contain all words of the German language
The idea behind is to check, which element of a list of random letter combinations of varying size is a proper German word
Any helpful comments appreciated
Horst
P.S.: A step forward: I tried IO::ALL and scalar @allwords reports 10 elements, means all words of the textfile are handed over into the array. But still no matches found.
|
|---|