Input from stdin for perl

I'm trying to take input from STDIN and push them in to an array.  I want the input to end when the user hits enter, but I can't seem to get it working correctly.  Here is what I've tried so far:

my @numbers;

while (<>) {
    s/\s+\z//;
    last if $_ eq "";
    push @numbers, $_;
}

I seem to get a blank array, any ideas?
bmcquillAsked:
Who is Participating?
 
ozoCommented:
What was the input from STDIN, and what made the array seem blank?
0
 
bmcquillAuthor Commented:
THe input was just 1,2,3,4,5
And I got back 0 when I tried to print the contents of the array.
0
 
ozoCommented:
How did you try to print the contents?
Was 1,2,3,4,5 all on one line?  Were there any lines before the 1,2,3,4,5?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
bmcquillAuthor Commented:
No lines before 1,2,3,4,5, entered all on one line.

I tried to print the array by print "@numbers\n" and print "$numbers\n" both.
0
 
ozoCommented:
echo "1,2,3,4,5" | perl -e '
my @numbers;

while (<>) {
    s/\s+\z//;
    last if $_ eq "";
    push @numbers, $_;
}
print "@numbers\n"
'

Open in new window

works for me, printing
1,2,3,4,5
as one array elwment
0
 
FishMongerCommented:
Is this what you're looking for?
echo "1,2,3,4,5" | perl -e '
my @numbers;
while (<>) {
    push @numbers, split /\D+/;
}
print "$_\n" for @numbers
'

Open in new window

Ountputs:
1
2
3
4
5
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.