in reply to Search element of array in another array

See the Basic debugging checklist: use Data::Dumper or Data::Dump to see what your data structures really look like. I'm guessing you will indeed find that your array contains only one element. The reason seems to be in the File::Slurp::Tiny docs: the documentation for read_file says: "Reads file $filename into a scalar. By default it returns this scalar.", implying that the entire file is read and returned as one scalar. You should probably try using the read_lines function instead. You should probably also turn on the chomp option.

Replies are listed 'Best First'.
Re^2: Search element of array in another array
by better (Acolyte) on Mar 26, 2015 at 20:08 UTC

    Thanks Anonymous Monk pointing to Data::Dumper. I checked both arrays

    The result show clearly, that the structure of the elements is different. An example:

    print Dumper @allwords;

    ...

    $VAR9 = 'hat

    ';

    ...

    print Dumper @cleanwords;

    ...

    $VAR6 = 'hat';

    ...

    I selected the word which should match. In @allwords the single quotation mark appears in a new line. Maybe it's caused by newline? and I should chomp the elements?

    I chomped it like this:

    chomp (@allwords);

    Result:

    ';AR9 = 'hat

    The single quotation mark of the predecessing word appears in the sam line.

      Please mark updates to your nodes, see here for why.

      I chomped it like this: ... ';AR9 = 'hat ... The single quotation mark of the predecessing word appears in the sam line.

      I'd guess the file is terminated by CRLF and your input record separator $/ is a plain LF. One approach to fix this is to set $/ = "\r\n"; before reading the file and the chomp. If you were using a regular open, you could use the :crlf I/O layer: open my $handle, '<:crlf', 'filename.txt' or die $!; (see PerlIO).

        ... and File::Slurp::Tiny should allow you to do this: read_lines("filename", chomp=>1, binmode=>":crlf")

        All correct!!!

        I put your line into my script and: TATA!!!! It works

        Hours

        and hours

        and hours....

        I love being a scripting amateur. ;-)

        Thanks anonymous monk, you safed my evening! :-))

      Maybe it's caused by newline? and I should chomp the elements?

      Yes and yes! :-)

      As mentioned below, try setting $Data::Dumper::Useqq=1; before calling Dumper.

Re^2: Search element of array in another array
by better (Acolyte) on Mar 26, 2015 at 19:52 UTC

    You are right.I tried

    read_lines

    before. But it didn't work either. Thanks for your hint.