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.


In reply to Search element of array in another array by better

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.