perl - Code not printing last element of array when using using CTRL+Z to end the STDIN input -
i seem have run peculiar problem. here code
#read list of strings , print in 20-character column print "enter strings:\n"; chomp(@list = <stdin>); foreach $_ (@list){ printf "\n%20s", $_; }
the code doesn't print last element of array if don't press enter before invoking end of file ctrl+z on windows.
edit: here's sample output
enter strings: v b v
here pressed ctrl-z after entering b , before pressing enter, , didn't print b. if had pressed enter ctrl-z, have printed b.
stdout line-buffered when going terminal; data doesn't shown until add newline. try:
print "enter strings:\n"; chomp(@list = <stdin>); print "\n"; foreach $_ (@list){ printf "%20s\n", $_; }
or adding $| = 1;
before loop.
Comments
Post a Comment