in reply to searching string from one file in another
Your post formatting is a bit off (and there's that trailing "This device belongs to the") but I've seen it updated once since I first opened it, so I suppose you are working on it. If you are having difficulties, you can look at Markup in the Monastery, or ask (maybe in the Chatterbox).
Anyway, the simplest way to check that a string is included in another is to use the index function (it will return the position of the match when there is one, or -1 when the substring can't be found). This would be something like:
The for loop can be written with grep instead: print $line if grep { index $line, $_ > -1 } keys %file2;. You should use whatever is easier for you to understand and editmy %file2; open my $file2, '<', shift or die; while ( my $line = <$file2> ) { chomp($line); # Do not keep the "\n" at the end of $line ++$file2{$line}; } open my $file1, '<', shift or die; while ( my $line = <$file1> ) { SEARCH: for my $search (keys %file2) { print $line and last SEARCH if index $line, $search > -1; # Ed +it: GotToBTru pointed out I forgot "index" } }
++ to hippo on the variable names (if you have to add a number to a variable name, it's probably not the right name).
Edit2: s/a bit of\K/f/; Thanks to Lotus1.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching string from one file in another
by BillKSmith (Monsignor) on Feb 28, 2017 at 17:36 UTC | |
|
Re^2: searching string from one file in another
by stray_tachyon (Initiate) on Aug 17, 2018 at 14:43 UTC | |
by Eily (Monsignor) on Aug 17, 2018 at 15:16 UTC |