in reply to need to remove the extra line in regular expression

This is what I would do:

my @devices; open (FILE,"config.txt") or die "cannot open file : $!"; # Always put a label on your loop when using next CONFIG_LOOP: while ( my $line = <FILE> ) { # Never use $_ directly if ( $line =~ /^(\w+)\s+/ ) { # Adding to the array push( @devices, "$1 => " ); } elsif ( $line =~ (/inet\saddr:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1 +,3})/) ) { # Adding the IP address to the last known device $devices[-1] .= "$1.$2.$3.$4\n"; } else { # Skipping other lines next CONFIG_LOOP; } } close (FILE); foreach my $device ( @devices ) { print $device; }
Take my advice. I don't use it anyway.

Replies are listed 'Best First'.
Re^2: need to remove the extra line in regular expression
by laksh (Novice) on Aug 22, 2012 at 22:00 UTC
    when I run the script I got the out put: eth0 => lo => But my expected output is eth0 => 192.168.8.76 lo => 127.0.0.1 and also please comment the line of code so that I can explain the co +de to my boss.