in reply to Re^4: Print word from text file that is not an exact match
in thread Print word from text file that is not an exact match

Hi Tony,

this is an example of how you could do it, by modifying your second while loop (untested):

while (my $line = <IN>){ my $server_name = (split /[<>]/, $line)[2]; foreach my $search (@computers) { print "Name ; $server_name\n" if $search eq $server_name; } }
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).

Change the first part of the script 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;
Then change the second while loop as follows:
while (my $line = <IN>){ my $server_name = (split /[<>]/, $line)[2]; print "Name ; $server_name\n" if exists $computers{$server_name}; }
The point is that a hash lookup (in %computers) is usually much faster than traversing a full array (@computers) every time.

Replies are listed 'Best First'.
Re^6: Print word from text file that is not an exact match
by TonyNY (Beadle) on Jun 12, 2018 at 01:45 UTC
    Thanks Laurent! I'll give it a try.