On Thu, Jul 24, 2008 at 1:02 AM, Noah <[EMAIL PROTECTED]> wrote:
>
>>
>> Untested code.
>>
>> # Always...
>> use strict;
>> use warnings;
>>
>> my %ip = ...;
>>
>> #Get the lines as an array.
>> my @lines = keys %ip;
>>
>> # Slight modification to the regex. Someone else's reply will probably
>> have a more elegant method.
>> my $ipSearch = qr/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/;
>>
>> # Use variables for filehandles.
>> open my $CHANGE, "> $changeFile" or die;
>> print CHANGE "NO\n" unless ( grep ( $ipSearch, @lines ) );
>>
>
> Sorry for all the emails.  Here is another thing I have a question about.
>
> is there a cleaner way to perform the following:
>
> my @lines = keys(%Input);
>
> print CHANGE "*** NO blah\n" unless ( grep ("blah", @lines) );
> print CHANGE "*** NO blah2\n" unless ( grep ("blah2 $any_ipaddress", @lines)
> );
> print CHANGE "*** NO blah3\n" unless ( grep ("blah3", @lines) );
> print CHANGE "*** NO blah4\n" unless ( grep ("blah4 $some_pattern", @lines)
> );
>


__CODE__
#! /usr/bin/perl

# Always...
use warnings;
use strict;

# Output file name
my $changeFile = "changes";

# Read input from <>
my @lines = ( <> );

# IP search pattern
my $ipSearch = '([0-9]{1,3}\.){3}[0-9]{1,3}';

my $pattern2 = '[0-9]{4}';

my %searches = ( blah  => "blah",
                 blah2 => "blah2 $ipSearch",
                 blah3 => "blah3",
                 blah4 => "blah4 $pattern2" );

# Open the output file
open my $FH, "> $changeFile" or die;

# Do the search
for my $search ( keys %searches ) {
    print $FH "$search\n" unless ( grep ( /$searches{ $search }/, @lines ) );
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to