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

Hi monks
I am making an array by reading a partiular file which contains information in the format
# SNMP manager 1 settings manager.1.address=127.0.0.1 manager.1.port=162 manager.1.version=2 manager.1.community=public
Now a user is typing a particular address lets say 127.0.0.1 Now i want to find that is there any manager with this adress and if its there then what is its index into the array.I am wriiting an approach but its not giving the index. lets say $address is user input.Plz help.
open(READ, "$var:$ENV{JBOSS_HOME}/conf/manager.properties"); my @contents = <READ> close(READ) foreach(@contents) { if(m/manager\.(\d*)\.address=(\d*\.\d*\.\d*\.\d*)/) { if($address eq "$2"){ $_ = "manager.$1.address=$2\n"; print "exist\n"; } } }

Replies are listed 'Best First'.
Re: to find an index of a particular value in an array
by Zaxo (Archbishop) on Sep 22, 2005 at 10:22 UTC

    Here's a simpler quicker way; instead of the foreach loop and pattern matches which succeed on each line,

    # my @lines = grep { -1 != index $_, $address } @contents; my @lines = grep { -1 != index $_, "=$address " } @contents; my @indexes = grep { -1 != index $content[$_], "=$address "; } 0 .. $#contents;
    That gives all the lines containing the target $address.

    Update: Skeeve++ is correct, substring amended. Added index search.

    After Compline,
    Zaxo

      But it doesn't give him the index and it will fail if the address is a substring of what is searched. Your code will (for example) return a line containing 201.1.1.102 when the user searched for 1.1.1.1

      $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: to find an index of a particular value in an array
by Skeeve (Parson) on Sep 22, 2005 at 11:30 UTC
    If you do this search regularly, why not create a hash of the ips?

    use strict; use warnings; my @contents= <DATA>; # indexing your content my %index; @index{ map((/address=(\d+\.\d+\.\d+\.\d+)/), @contents) }= 0..$#conte +nts; # beware! will fail if no address in a line! # retrieving an index my $i= $index{'127.0.0.1'}; print defined($i) ? $i : "not found"; __DATA__ # SNMP manager 1 settings manager.1.address=127.0.0.1 manager.1.port=1 +62 manager.1.version=2 manager.1.community=public # SNMP manager 1 settings manager.1.address=127.0.2.1 manager.1.port=1 +62 manager.1.version=2 manager.1.community=public # SNMP manager 1 settings manager.1.address=127.0.3.1 manager.1.port=1 +62 manager.1.version=2 manager.1.community=public

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: to find an index of a particular value in an array
by albert (Monk) on Sep 22, 2005 at 10:21 UTC
    You can change your loop code to be as follows:
    for($i = 0; $i < @contents; $i++){ if($contents{$i] =~ m/manager\.(\d*)\.address=(\d*\.\d*\.\d*\.\d +*)/) { if($address eq "$2"){ print "manager.$1.address=$2\n"; print " exist at index $i\n"; } } }
    -albert

    Update: editing the suggestion for the lack of $_ in the solution. Untested code, your mileage may vary.

Re: to find an index of a particular value in an array
by blazar (Canon) on Sep 22, 2005 at 11:34 UTC
    Zaxo suggested a particularly neat way to get all the lines containing the address you want. Which is indeed what I'd look for too. But if you really want the index instead, just change that code to
    my @indexes = grep { -1 != index $contents[$_], $address } 0..$#conten +ts;
    Of course this is much less neat, but then you may adapt any of these techniques to your own needs...
Re: to find an index of a particular value in an array
by graff (Chancellor) on Sep 22, 2005 at 21:15 UTC
    The success/failure of the search for a given IP address will also depend on how you are assigning a value to $address. You say "a user is typing" this value -- are you reading it from STDIN? If so, to you "chomp" the string that you read, to remove the line-termination character(s)?

    Maybe you should check for (and remove) leading and trailing whitespace in the user input, which would be a suitable alternative to using chomp:

    print "Enter IP address to locate: "; $address = <STDIN>; $address =~ s/^\s*//; $address =~ s/\s*$//;
    OTOH, if the user is providing the IP address via @ARGV (as a command-line argument), then you don't need to worry about any of that.