in reply to Re^6: String comparison in an array
in thread String comparison in an array

I have updated the code with few of your comments
The solution was in the last one though. Your analysis was correct (at least it's part of the issue), the fact that @fillarr contains quotes ("computer" rather than just computer) makes the comparison fail. You should create your $csv object like this: my $csv = Text::CSV->new ({ binary => 1, sep_char => ";" }); (the sep_char part is required). And then replace my (@fillarr) = split ';',$line; by using $csv->parse on $line, and then using $csv->fields.

Also please, replace your first open by open my $types, "<", "types.txt" or die $!; then read with <$types>

And even if the code doesn't work as you want, please make sure that the code you post here at least compiles ;-)

Edit: oh and LanX is right, it would be far easier for us to help you with data (ie, an example of what your two files look like). But that's part of what you need for a short, working example (so again, what hippo asked).

Replies are listed 'Best First'.
Re^8: String comparison in an array
by newperlbie (Acolyte) on Aug 08, 2018 at 14:52 UTC
    Thanks Eily for the explanation.I am going to clean my code. Meanwhile I tried this and it worked....but is this really a bad piece of code?when there is a huge file?
    foreach(@keys) { if ( $fillarr1[14] =~ /$_/) { #Found }

      It will probably work most of the time. But you will get the wrong result in some cases. For example if $_ is com, and $fillarr1[14] is computer you will get a match even though the two words are not exactly the same. You will also run into trouble if $_ contains special characters.

      There are ways to solve those issues, but the best solution is to not have " inside $fillarr1[14]. Now either you remove them, or you try to understand how Text::CSV works and put the data into @fillarr by using it.

        Good. Thanks again!I will explore Text::CSV