Your code seems to "work" for me. I enter 1,2,3, and I get 3,2,1:
use warnings;
print "Enter some lines, then press Ctrl-D:\n"; # or maybe Ctrl-Z
@lines = <STDIN>;
@reverse_lines = reverse (@lines);
print @reverse_lines;
__END__
Enter some lines, then press Ctrl-D:
1
2
3
3
2
1
If you enter a palindrome, like 1,2,1, then the reverse will look the same as what you entered. | [reply] [d/l] |
Au contraire, it seems to, indeed, reverse the order of the lines:
$ perl rev.pl
Enter some lines, then press Ctrl-D:
test
123
done
Output:
done
123
test
Perhaps you are interpreting the meaning of "reverse" differently. What, exactly, are you expecting for output?
(By the way, it is good practice to use warnings; use strict; in your perl programs. Doing so will save you time in the long run by catching silly mistakes while improving your coding habits.) | [reply] [d/l] [select] |
Hi gaurav,
...it should return the reverse of whatever the list has been entered by the user...
True if and only if the user didn't enter all the list on a single line on the CLI. Because with that the array will only have one "value". See what I mean below, using a modified toolic example.
use warnings;
use strict;
print "Enter some lines, then press Ctrl-D:\n"; # or maybe Ctrl-Z
my @lines = <STDIN>;
my @reverse_lines = reverse (@lines);
print @reverse_lines;
__END__
Enter some lines, then press Ctrl-D:
1 2 3 # entered by the user
1 2 3 # printed back
But if otherwise, your script should work as you intended. Mine observation please.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [d/l] |
Hi Monks,
Sorry for being late.
Another thing is, I have been typing all my inputs in a single line that's why not getting the desired result.
But thanks to 2teez ,now I am getting right output.
And thanks to all Of you for help me out this.
| [reply] |
Did you figure this out? I'm curious as to what the problem was. | [reply] |