Perl文本解析变量无法打印

时间:2017-04-12 20:33:03

标签: perl text-parsing

我有这个perl程序,应该转换单位,英寸到厘米等。但它不会在循环期间打印变量。它只打印标准“等于”并说$ unit2未初始化

Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 1.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 2.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 3.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 4.
 is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 5.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 6.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 7.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 8.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 9.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 10.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 11.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 12.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 13.
  is equal to  
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 14.
  is equal to  

这是我的代码

#!/usr/bin/perl
#usage: perl convert.pl

use Path::Class;
use warnings;
use autodie; # die if problem reading or writing a file

my $dir = dir("./");

my $file = $dir->file("distances.txt");

# Read in the entire contents of a file
my $content = $file->slurp();

# openr() returns an IO::File object to read from
my $file_handle = $file->openr();

# Read in line at a time
while( my $line = $file_handle->getline() ) {
    # parses string into value and unit
    my ($val, $unit) = split /[\n]/, $line;
    # initialize values
    my ($val2, $unit2) = "";
    # check unit and apply corresponding math
    if ($unit eq "in") {
        $val2 = $val * 2.54;
        $unit2 = "cm";
    } 
    else {
        if ($unit eq "ft") {
            $val2 = $val * 0.3048;
            $unit2 = "m";
        } 
        else {
            if ($unit eq "yd") {
                $val2 = $val * 0.9144;
                $unit2 = "m";
            } 
            else {
                if ($unit eq "mi") {
                    $val2 = $val * 1.609347;
                    $unit2 = "km";
                }#close mile if statement
            }#close else statement
        }#close 2nd else statement
    }#close 1st else statement
    print $val, " ", $unit, " is equal to ", $val2, " ", $unit2, "\n";
}

这是我正在使用的文本文件。

74 in
10 in
10 ft
10 yd
10 mi
72 in
6 ft
2 yd
300 ft
100 yd
158400 in
13200 ft
4400 yd
2.5 mi

1 个答案:

答案 0 :(得分:1)

while( my $line = $file_handle->getline() ) {
    # parses string into value and unit
    my ($val, $unit) = split /[\n]/, $line;

getline返回一行。通过将其拆分为换行符[\n],您可以在$val中获取该行,而在unit中找不到该行。也许你想要split ' '

使用哈希进行单位转换可能会显着缩短代码,使其更具可读性:

my %units = ( in => [ cm => 2.54 ],
              ft => [ m  => .3048 ],
              yd => [ m  => .9144 ],
              mi => [ km => 1.609347 ],
);

while (my $line = $file_handle->getline) {
    my ($val, $unit) = split ' ', $line;
    my ($val2, $unit2) = ($units{$unit}[1] * $val, $units{$unit}[0]);
    print "$val $unit is equal to $val2 $unit2\n";
}