vsailas has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a strange problem.
I need to insert an ip address entered (entered for updation by existing users)if it does not already exist in database and
update the creator(just a REMOTE_USER field) if the ip given is a sub set of existing entries in database.

And if the customer is brand new person.. a new entry in a database.
Now My problem is with this code For a brand new user, two entries show up.
Also updation is all wrong all the time new entry is done.
update=1 if already a user else its 0;
$begin1 and $end are entries made by user a pure integer format without any '.' like 127001 for 127.0.0.1
Please help me!

if( $update eq 1) { my $sth= $dbh->prepare("select ip_start, ip_end from ip_range wher +e subscription_no =?"); $sth->execute($subno); my @row; while (@row = $sth->fetchrow_array()) { my($start, $stop)= @row ; $startId=$start; $start=~ s/\.//gi; $stop=~ s/\.//gi; if (($begin1 >= $start) && ($end1 =< $stop)) { $ipFlag=1; } } $sth->finish(); } else { ### insert into table, the fresh data. $host="you"; my $sth = $dbh->prepare("insert into ip_range(subscription_no, ip_ +start, ip_end, hostname, creator, creationdate) values(?,?,?,?,?,now( +))"); $sth->execute($subno, $begin, $end, $host, $creator); $sth->finish(); } # update only Creator since ip already exist. if( $ipFlag eq 1) { my $sth= $dbh->prepare("update ip_range set hostname=?, creator=? +where subscription_no =?"); $sth->execute($host, $creator, $subno); $sth->finish(); } ### insert into table, the fresh data. else { $host="me"; my $sth = $dbh->prepare("insert into ip_range(subscription_no, ip_ +start, ip_end, hostname, creator, creationdate) values(?,?,?,?,?,now())"); $sth->execute($subno, $begin, $end, $host, $creator); $sth->finish(); } $dbh->disconnect();

Thanks!

Replies are listed 'Best First'.
Re: Please help me with this conditional loop on text entries.
by poolpi (Hermit) on Dec 14, 2007 at 08:55 UTC
    you could perhaps format the input in a first time and stock in your database the IP address in hexadecimal format (?)
    #!/usr/bin/perl use strict; use warnings; use Regexp::Common qw /net/; use IO::Prompt; my $ip; # formatting the input while ( $ip = prompt "Enter a valid IP address: ", -until => qr/^$RE{n +et}{IPv4}{-sep=> '.'}$/ ) { print "Your say: '$_'\n"; } # dotted-quad format to hexadecimal my $hex_ip = unpack "H*", pack "C4", split '\.', $ip;
    HTH,
    PooLpi
    ps : sorry for my poor english !
      Thank you you guys had been of great help.
Re: Please help me with this conditional loop on text entries.
by lidden (Curate) on Dec 14, 2007 at 07:03 UTC
    You cannot just remove the dots like you are doing. That will make different numbers like 127.0.0.1 and 12.70.0.1 look the same. You could expand the numbers to all be three digit numbers, remove the dots and then compare them, but I am sure someone will give a better solution.

      Here is how to convert from a dot quad to the real unsigned 32 bit integer it represents and back again

      use Socket; for my $dot_quad ( "0.0.0.1", "127.0.0.1", "192.168.0.1" ) { my $integer = unpack "N", inet_aton($dot_quad); my $ip = inet_ntoa(pack "N", $integer); print "$ip\t int:$integer\n"; }