Perl Forum for Beginners

Searching keys

Jul 24, 2008 3:59 am
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

Jul 24, 2008 4:07 am
Yitzle
Re: searching keys

On Thu, Jul 24, 2008 at 3:59 AM, Noah <admin2@enabled.com> 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 ) );


Jul 24, 2008 4:56 am
Noah
Re: searching keys



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


Jul 24, 2008 4:44 am
Noah
Re: searching keys



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




Previous Thread: How to convert the date ?
Next Thread: Diamond operator and quoted word?

Related Forum Topics
Ways to get a mapping of values versus keys in a hash where there is one-to-one mapping between the keys and the values !
------=_Part_6250_27861417.1227632496175
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi all,

I recently had one requirement where I needed to get the key in a hash on
the basis of supplied value.

There is one additional...
RegExp Searching within
--0-1241276779-1231171365=:89023
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi:

Given the following list:
<elem1>
<elema></elema>
<elemb>
<elemc></elemc>
</elemb>
</elem1>

I=A0 want to know all the "elements" within elem1. (Note: It...
Query in searching
--0016364177e1beb3930465e19eb6
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Hi,

I am trying to search for a pattern in a file in my perl script.
Syntax used is :

system("grep \"$res\" ${data_dir}/${node}.load...
Automatic searching
--0016e686cd3a11082e0490ff43d5
Content-Type: text/plain; charset=GB2312
Content-Transfer-Encoding: quoted-printable

Dear everyone,

In my file, each line has : ID (8 number) | many key words |key words again
.
I would like to know how to extact only key words part and...
Searching the array
--000e0cd2dbe888eee904ab122426
Content-Type: text/plain; charset=ISO-8859-1

Hi!
I want to search whether a scalar '$a' contains any one of value of an array
'@arr'.
How it may be done?
as its not correct : print "found" if $a =~ /@arr/;

--000e0cd2dbe888eee904ab122426--


A problem of searching through directories
I have encounter a problem in which i need to scan over all the files
within a directory tree. My script is like the following

#!/usr/bin/perl -w
$d = "some directory here";

chop($d);
&searchDirectory($d);

sub searchDirectory {
local($dir);
local(@lines1);
...
Searching Hash for contents of Array
------=_NextPart_000_010B_01CD26F8.45A63DC0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hopefully I have the correct mailing list for this question.



I have a script that works and looks as follows:

-------

#$hash{SRC} is filled in by a...
Searching arrays - more optimal code
Hi List,

Is there a more optimal way to present this data structure without
becoming completely convoluted?

--- snip ---


for my $key (sort keys %usernames) {
my $found = 0;
for my $knownusername (@knownusernames) {
if ($knownusername eq $key)...
Searching a script that use HTTP::Proxy
Moin,

as my first perl project I want to write a http proxy, that can modify
headers. Because I don't belief that there is already such a script I'm
searching for a simple proxy that use HTTP::Proxy as its basis.

Any hint?

Thanks
--
|Michael Renner E-mail:...
Hash keys
Hi All,


Why this following code has not working as expected?

print "Number of element(s) : " . sprintf("%10d", keys(%hash) ) . "\n";

Thanks.

--budhi