943,812 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 829
  • Perl RSS
Jun 28th, 2009
0

Perl Newby Question

Expand Post »
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:

Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl -w
  2. #Exercise 4.7.
  3.  
  4. #Assign filename to $filename variable.
  5. $filename = 'list.txt';
  6.  
  7. #Open file and assign filehandle.
  8. open(LISTFILE, $filename);
  9.  
  10. #Read file into array.
  11. @filearray = <LISTFILE>;
  12.  
  13. #Close the file.
  14. close LISTFILE;
  15.  
  16. #Assign the length of the array to scalar variable.
  17. $a = @filearray;
  18.  
  19. #Initialise $count variable.
  20. $count = 0;
  21.  
  22. #Loops through array and pops off the last item and prints it.
  23. while($a > $count)
  24. {$count++;
  25. $item1 = pop @filearray;
  26. print $item1;
  27. }
  28.  
  29. exit;

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:

Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl -w
  2. #Exercise 4.7.
  3.  
  4. #Assign filename to $filename variable.
  5. $filename = 'list.txt';
  6.  
  7. #Open file and assign filehandle.
  8. open(LISTFILE, $filename);
  9.  
  10. #Read file into array.
  11. @filearray = <LISTFILE>;
  12.  
  13. #Close the file.
  14. close LISTFILE;
  15.  
  16. #Assign the length of the array to scalar variable.
  17. $a = @filearray;
  18.  
  19. #Initialise $count variable.
  20. $count = 0;
  21.  
  22. #Loops through array popping off the last item and printing it.
  23. #If statement solves the problem with the first two items.
  24. while($a > $count)
  25. {$count++;
  26. $item1 = pop @filearray;
  27. if ($count == 1)
  28. {
  29. print $item1, "\n";
  30. }
  31. else {
  32. print $item1;
  33. }
  34. }
  35.  
  36. exit;

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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
binfPerl is offline Offline
4 posts
since Jun 2009
Jun 28th, 2009
0

Re: Perl Newby Question

OK, now the easy way......


Perl Syntax (Toggle Plain Text)
  1. open(LISTFILE, $filename);
  2. @filearray = <LISTFILE>;
  3. close LISTFILE;
  4. print "$_\n" for reverse @filearray;
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jun 28th, 2009
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
binfPerl is offline Offline
4 posts
since Jun 2009
Jun 28th, 2009
0

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.


Perl Syntax (Toggle Plain Text)
  1. open(LISTFILE, $filename);
  2. @filearray = <LISTFILE>;
  3. chomp @filearray;
  4. close LISTFILE;
  5. print "$_\n" for reverse @filearray;
  6. \
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jun 29th, 2009
0

Re: Perl Newby Question

Ah, I understand now.

Thanks for your answer to my problem and your improvement of my code, Kevin!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
binfPerl is offline Offline
4 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Simple Benchmark script
Next Thread in Perl Forum Timeline: array problem - help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC