For a multiline input, if the user leaves a blank line to terminate, use a loop instead:
Code:
my @arr = ();
while (<STDIN>) {
chomp($_);
last if ($_ eq '');
push(@arr, $_);
}
foreach (@arr) {
printf "%s\n", $_;
}
printf "Number of elements in \@arr: %d\n", scalar(@arr);
The chomp function strips the trailing newline from the input, before pushing it into the array.
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.