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

Hi, i want to display specific ip address that is stored in txt file near keyword "a.AS1", which i am taking from user input but my code is not working.

open FILE , "routers.txt" or die $!; while (<FILE>) { print "$_\n"; } print " Enter name"; $s=<>; chomp($s); while(my $line = <FILE>) { #if(/\b$s\b/){ if(/\b$s\b/){ my $IP = $line =~ /\d+\.\d+\.\d+\.\d+/; push(@IPS, $IP); } } #print "hei"; print $IP;

text file contents are here

a.AS1 10.0.0.128 b.AS1 10.0.0.129 c.AS1 10.0.0.130 d.AS1 10.0.0.131 e.AS1 10.0.0.132

Replies are listed 'Best First'.
Re: Cant help print key word
by marto (Cardinal) on Aug 28, 2012 at 15:32 UTC

    "Not working" isn't an error report. As advised in answers to some of your previous questions (e.g. No Such File or Directory Error) you could add use strict; use warnings; and start debugging from there to find out what is going wrong and why.

Re: Cant help print key word
by Kenosis (Priest) on Aug 28, 2012 at 15:53 UTC

    Great suggestions have been given. Here's are some other observations/suggestions:

    • Use a three-argument open.
    • Use lexical file handles, e.g., my $fh, instead of FILE.
    • Your first while reads all lines from the file, so there are none left for the second while
    • Use lexical variables throughout.
    • Your if(/\b$s\b/) matches against $_ when you've assigned a file line to $line.
    • close the file when your done with it.

    Give the above, consider the following:

    use strict; use warnings; my ( @IPS, $IP ); print "Enter name:\n"; my $s = <>; chomp $s; open my $fh, '<', 'routers.txt' or die $!; while ( my $line = <$fh> ) { if ( $line =~ /\b$s\b/ ) { my ($IP) = $line =~ /(\d+\.\d+\.\d+\.\d+)/; push( @IPS, $IP ); } } close $fh; print "$_\n" for @IPS;

    Hope this helps!

Re: Cant help print key word
by aitap (Curate) on Aug 28, 2012 at 15:31 UTC

    Well, firstly you read and print the entire file and then try to search in the remaining parts of the file (i.e nowhere). If you really want to work this way, you should seek to the beginning of the file.

    Also, your $IP variable is declared inly inside the while loop. Declare it earlier (outside the loop) or print something from @IPS instead.

    Also, you need another syntax for your matching:

    ($IP) = $line =~ /(\d+\.\d+\.\d+\.\d+)/;
    Pattern matching returns an array, and you should assign an array (anonymous array is fine) if you want to get what matched (not the count of matches = array in scalar context).

    Sorry if my advice was wrong.
Re: Cant help print key word
by philiprbrenan (Monk) on Aug 28, 2012 at 15:31 UTC

    I think it would be easier if you used index() because the IP addresses have '.'s in them which mean 'match any character'.

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my $i = "10.0.0.132"; say for grep {index($_, $i) != -1} split /\n/, << 'END'; a.AS1 10.0.0.128 b.AS1 10.0.0.129 c.AS1 10.0.0.130 d.AS1 10.0.0.131 e.AS1 10.0.0.132 END

    Produces

    e.AS1    10.0.0.132