The code you posted doesn't compile. Please post a compiling code snippet and also a sample of what you have in /etc/services. (See Write-up formatting tips.)

Here's a rewrite of your code with several comments so you can see what's different:

( Edit: as hippo points out below, it's not likely that your /etc/services file actually contains lines containing only port numbers. This example code does not address that issue, nor try to use regular expression matching; it's just an example of how to work through your two data sets. )

#!usr/bin/perl use strict; # don't leave home without it use warnings; # same here! use 5.10.0; # so you can use say() my @ports = qw( 21 22 23 ); # make a hash from the array for easier lookup my %ports = map { $_ => 1 } @ports; # use a lexical variable for your filehandle open my $SERVICES_FILE, '<', '/etc/services' or die $!; # read one line at a time with while() while ( my $line = <$SERVICES_FILE> ) { # deal with new-line character at end of line chomp( $line ); # 'postfix if' simplifies syntax say "Match Found: $line" if $ports{ $line }; } # good practice to close filehandle close $SERVICES_FILE or die $!; # good practice to tell Perl when the program's done __END__

The way forward always starts with a minimal test.

In reply to Re: Port check - Services File by 1nickt
in thread Port check - Services File by deelinux

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.