![]() |
| ||
| Perl Newby Question Hello, I'm fairly new to PERL and this is my first post - so be kind. I'm working through a book called Beginning PERL for Bioinformatics, and one of the exercises asks to "Write a program to read a file, and then print its lines in reverse order, the last line first". I've solved the problem, but something kept happening that I didn't understand. When I used this: #!/usr/bin/perl -w it did the job of reversing my list, but kept putting the last item in my list directly next to the second to last item, but those that followed where on new lines. Sorry that I'm not explaining it too well, here is what I mean. If my list contains the words: dog onion treaty frogs fish when I reverse it, I get fishfrogs treaty onion dog I solved the problem using: #!/usr/bin/perl -w but I would like to know what causes the problem with the first two items on the list. I had the same problem when I just reversed the array and assigned it to a new array. Thanks for any help you can give! |
| ||
| Re: Perl Newby Question OK, now the easy way...... open(LISTFILE, $filename); |
| ||
| Re: Perl Newby Question Thanks very much, Kevin. I suspect I'll be doing things the hard way until I've written a few more PERL programs. Still, even your more elegant script seems to be having a slightly different effect on the first two items it deals with. So, using the list from my question, I now get: fish frogs treaty onion dog It gives a blank line after all the items, except fish and frogs. I know it isn't really a big deal, I'm just curious why. |
| ||
| Re: Perl Newby Question We are adding a newline to the output when the lines of the file already have a newline at the end so we create double-spacing for any line that already has a new line. Sometimes the last line of a file does not have a newline on the end so if you print it in reverse order the first and second lines (of the reversed order) appear to be on the same line. The safest thing to do is use chomp() and print the newline during the output. open(LISTFILE, $filename); |
| ||
| Re: Perl Newby Question Ah, I understand now. Thanks for your answer to my problem and your improvement of my code, Kevin! |
| All times are GMT -4. The time now is 2:06 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC