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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.