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
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}; }
|
|---|
| 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 |