2017-04-12 5 views
0

단위, 인치 및 센티미터 등으로 변환해야하는이 perl 프로그램이 있습니다. 그러나 루프 중에 변수를 인쇄하지는 않습니다. 그것은 단지 표준 "같다"인쇄와 $은 Unit2는 여기에 내 코드 여기Perl 텍스트 분석 변수가 인쇄되지 않습니다.

#!/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"; 
} 

내가 사용하고 텍스트 파일입니다

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 

초기화되지 않은 것을 말한다.

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 
+0

'사용 경고;' – toolic

+0

https://metacpan.org/pod/Math::Units – toolic

+0

* ** 항상 *** 엄격한 사용 및 경고 사용 'all'' – Borodin

답변

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"; 
} 
+0

정말 고마워요! – Josh

+0

@ Josh : 업데이트도 확인하십시오. – choroba

+0

@mkHun : 아니에요, 그게 핵심입니다. – choroba