Re: searching keys



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



Relevant Pages

  • searching keys
    ... 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. ...
    (perl.beginners)
  • Re: "index" efficiency... any help or ideas?
    ... > dealing with different searching algorithms. ... > cons of hashing vs. binary searching. ... That's the killer reason for using hashes in this application. ... Searching a hash table is ...
    (alt.lang.asm)
  • A hash or array of regexps?
    ... I often find myself with a list of things that I'm searching for. ... I build a hash with the ... key being the four-character pattern, ... as quick or efficient as simple keys. ...
    (comp.lang.perl.misc)
  • Re: problem with large hash vanishing
    ... > configuration file into a hash of hashes. ... Depending on the format of the config file, you may be able to use a CPAN ...
    (comp.lang.perl.misc)
  • Re: Linux and audio pro
    ... >> filesystem, so there's nothing to get out of synch. ... > configuration information in a database. ... config file, for the app to start off with, and fine tuned by dynamic ... Additionally, things like postfix, can use db hash files (think ...
    (comp.os.linux.misc)