this is an example of how you could do it, by modifying your second while loop (untested):
Please note, however, that using a hash, rather than an array, for storing the computers would certainly be more efficient (especially if you have a lot of data).while (my $line = <IN>){ my $server_name = (split /[<>]/, $line)[2]; foreach my $search (@computers) { print "Name ; $server_name\n" if $search eq $server_name; } }
Change the first part of the script as follows:
Then change the second while loop as follows:my %computers; open FILE, '<', $namefile or die "Could not open $namefile : $!"; while (<FILE>){ chomp; s/^\s+|\s+$//g; # trim spaces next unless /\S/; # skip blank lines $computers{$_} = 1; } close FILE;
The point is that a hash lookup (in %computers) is usually much faster than traversing a full array (@computers) every time.while (my $line = <IN>){ my $server_name = (split /[<>]/, $line)[2]; print "Name ; $server_name\n" if exists $computers{$server_name}; }
In reply to Re^5: Print word from text file that is not an exact match
by Laurent_R
in thread Print word from text file that is not an exact match
by TonyNY
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |