FAQ
hi

i m faced with a problem.

i have a string set to an IP address and i want to be able to compare it with
just the first three octets and see if it matches. How do i go about?
I know regex can do it but the problem is i m a newbie to it. If anyone can
figure out and explain the regex for it, it will be extremely beneficial.
thanks!

- --
Jasmine Chua

Search Discussions

  • Yargo at Mar 29, 2003 at 5:13 pm
    Simple:

    #!/perl

    my $IP_Address = "123.456.789.000";
    my $Other_IP = "123.456.789.001"; # or STDIN or whatever.
    my ($A1, $B1, $C1, $D1) = (split /\./, $IP_Adress);
    my ($A2, $B2, $C2, $D2) = (split /\./, $Other_IP);
    if ($A1 == $A2 && $B1 == $B2 && $C1 == $C2) {
    print "IP confirmed";
    } else {
    die "Wrong IP address\n";
    }


    -----Original Message-----
    From: Jasmine
    Sent: Saturday, March 29, 2003 6:36 PM
    To: beginners@perl.org
    Subject: perl help!!


    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    hi

    i m faced with a problem.

    i have a string set to an IP address and i want to be able to compare it
    with
    just the first three octets and see if it matches. How do i go about?
    I know regex can do it but the problem is i m a newbie to it. If anyone can
    figure out and explain the regex for it, it will be extremely beneficial.
    thanks!

    - --
    Jasmine Chua

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • John W. Krahn at Mar 29, 2003 at 8:52 pm

    Jasmine wrote:

    hi Hello,
    i m faced with a problem.

    i have a string set to an IP address and i want to be able to compare it with
    just the first three octets and see if it matches. How do i go about?
    I know regex can do it but the problem is i m a newbie to it. If anyone can
    figure out and explain the regex for it, it will be extremely beneficial.
    Here is one way to do it:

    use Socket;

    print "Matched\n" if substr( inet_aton( $ip1 ), 0, 3 ) eq substr( inet_aton( $ip2 ), 0, 3 );



    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 29, '03 at 4:37p
activeMar 29, '03 at 8:52p
posts3
users3
websiteperl.org

3 users in discussion

Yargo: 1 post Jasmine: 1 post John W. Krahn: 1 post

People

Translate

site design / logo © 2022 Grokbase