Re: searching keys

2008-07-24 Thread John W. Krahn

Noah wrote:


okay I have a strange issue.  all the lines as part of "my @lines = keys 
%ip;" have a space at the beginning of the line of each key.


perldoc -q "Why do I get weird spaces when I print an array of lines?"


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: searching keys

2008-07-24 Thread Mr. Shawn H. Corey
On Thu, 2008-07-24 at 10:06 -0400, yitzle wrote:
> On Thu, Jul 24, 2008 at 12:44 AM, Noah <[EMAIL PROTECTED]> wrote:
> >
> > Hi there,
> >
> > okay I have a strange issue.  all the lines as part of "my @lines = keys
> > %ip;" have a space at the beginning of the line of each key.
> >
> > cheers,
> >
> > Noah
> 
> Without knowing how %ip was formed or how you are deciding there is a
> space, this is hard to "solve".
> 

The problem is that:

  print "@lines";

and:

  print @lines;

do two different things.  There are no leading spaces in @lines; the
double quote places them there.

See `perldoc perlvar` and search for '$LIST_SEPARATOR'



-- 
Just my 0.0002 million dollars worth,
  Shawn

"Where there's duct tape, there's hope."

"Perl is the duct tape of the Internet."
Hassan Schroeder, Sun's first webmaster


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




Re: searching keys

2008-07-24 Thread yitzle
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/




Re: searching keys

2008-07-24 Thread yitzle
On Thu, Jul 24, 2008 at 12:44 AM, Noah <[EMAIL PROTECTED]> wrote:
>
> Hi there,
>
> okay I have a strange issue.  all the lines as part of "my @lines = keys
> %ip;" have a space at the beginning of the line of each key.
>
> cheers,
>
> Noah

Without knowing how %ip was formed or how you are deciding there is a
space, this is hard to "solve".

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




Re: searching keys

2008-07-23 Thread Noah




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




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




Re: searching keys

2008-07-23 Thread Noah



yitzle 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 ) );




Hi there,

okay I am trying to figure this out.
 when I perform the following:


there is a space at the beginning of each line printed:
print "@lines";


there is no space at the beginning of the line:
for $line (@lines) {
print "$line";
}

Cheers,
Noah

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




Re: searching keys

2008-07-23 Thread Noah




Noah


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




Hi there,

okay I have a strange issue.  all the lines as part of "my @lines = keys 
%ip;" have a space at the beginning of the line of each key.


cheers,

Noah

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




Re: searching keys

2008-07-23 Thread yitzle
On Thu, Jul 24, 2008 at 3:59 AM, Noah <[EMAIL PROTECTED]> wrote:
>
> Hi there,
>
> I loaded a bunch of configuration lines into a hash called $Input
> now I am searching the hash to make sure specific lines are there.
>
> I want to print "NO" unless there is a match to the following ""check $ip
> here";
>
> but $ip could be the range as shown below.
>
> so a key like "check 2.2.2.2 here" would be accepted and NO would *not* be
> printed and if the hash has a key with "check 2 here" then a NO *would* be
> printed.
>
> my $ip = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+";
> print CHANGE "NO\n" unless exists $Input{"check $ip here"};
>
>
> Cheers,
>
> Noah

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

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




searching keys

2008-07-23 Thread Noah


Hi there,

I loaded a bunch of configuration lines into a hash called $Input
now I am searching the hash to make sure specific lines are there.

I want to print "NO" unless there is a match to the following ""check 
$ip here";


but $ip could be the range as shown below.

so a key like "check 2.2.2.2 here" would be accepted and NO would *not* 
be printed and if the hash has a key with "check 2 here" then a NO 
*would* be printed.


my $ip = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+";
print CHANGE "NO\n" unless exists $Input{"check $ip here"};


Cheers,

Noah

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