2017-05-30 90 views
-2

它的更多鈔票在Perl輸出代替打印結果Simple.csv的Perl的打印循環和更換打印結果之前

內容:

string1 
string2 
string3 

我的腳本:

#!/usr/bin/perl 
use strict; 
use warnings; 
my $file = 'simple.csv'; 
open my $info, $file or die "Could not open $file: $!"; 
while(my $line = <$info>) { 
    sleep(2); 
    print $line ; 
} 

close $info; 

輸出它像:

string1 
string2 
string3 

如何更改單行輸出替換string1 ..然後替換string2 ...然後替換字符串3

+1

對不起,我認爲你需要澄清這個問題。你究竟想要做什麼以及你已經嘗試過什麼? – Daz

+0

感謝您的迴應,所以我想輸出像替換 – bambeach

+0

輸入請... ...? – ssr1012

回答

1

使用printf來控制拼版換行符。使用退格鍵(\b)向後移動到最後一個輸出行上,或者直接發出回車符(\r)移動到行首。線路緩衝也被禁用。像這樣的東西符合你的要求(據我所知):

#!/usr/bin/env perl 
use strict; 
use warnings; 
my $file = 'simple.csv'; 
open my $info, '<', $file or die "Can't open '$file': $!\n"; 
$|++; #...don't buffer output... 
while (my $line = <$info>) { 
    chomp $line; #...remove ending newline... 
    # printf "%s%s", $line, "\b" x length($line); # alternative-1 
    printf "%s\r", $line;      # alternative-2 
    sleep 2; 
} 
close $info; 
print "\n";   #...leave a clean output... 
+0

是+1謝謝你,這就是我的意思 – bambeach

+0

@bambeach如果這解決了你的問題,請將答案標記爲已接受並向上投票 – JRFerguson

0

希望,打印聲明本身可以做到這一點。

#!/usr/bin/perl 
use strict; 
use warnings; 
my $file = 'simple.csv'; 
open my $info, $file or die "Could not open $file: $!"; 
while(my $line = <$info>) { 
     chomp($line); 
     print "$line\r"; 
     sleep(2); 
} 

close $info; 

此回車符移動到每行打印的行首。