- 论坛徽章:
- 0
|
怎样按照我输入的编号去读取一行数据。
# This script receive a number from console then extract a IP address corresponding to the number.As following:
# somefile.ini
# 1 210.102.13.17
# 2 102.13.20.8
# If the input is "2",then "102.13.20.8" is what I want.
print "Input a number:";
$n = <STDIN>;;
chop($n);
print "You input number :" . $n;
open( HFILE, "somefile.ini" );
while ($line = <HFILE>
{
@fields = split( /\s+/, $line );
if ( $fields[0] == $n )
{
$target = $fields[1];
last;
}
}
print "\nThe corresponding IP is " . $target;
测试文件somefile.ini:
1 210.102.13.17
2 102.13.20.8
3 192.168.1.2
4 23.67.222.110 |
|