By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,811 Members | 1,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,811 IT Pros & Developers. It's quick & easy.

program for input apple output "apple"

P: 4
my frogram for it
Expand|Select|Wrap|Line Numbers
  1. #!usr/local/bin/perl
  2. #program practice
  3. $file='input.txt';
  4. open(FH, $file);
  5. @line=<FH>;
  6. open(OUT, ">>output.txt");
  7. foreach $file(@line)
  8. {
  9. $r="#$file#";
  10. print(OUT "$r");
  11. }
  12.  
  13. close(FH);
  14.  
  15.  
  16. input i gave
  17. apple 
  18. peach
  19. orange
  20. watermelon
  21.  
  22. output is coming like this
  23. #apple
  24. #peach
  25. #orange
  26. #watermelon
  27.  
  28. instead i want like this
  29. #apple#
  30. #peach#
  31. #orange#
  32. #watermelon#
  33.  
plz help
plz
Jan 15 '10 #1
Share this Question
Share on Google+
1 Reply


nithinpes
Expert 100+
P: 410
when you read from the file, $file contains a newline. Remove the newline before appending the #.

Expand|Select|Wrap|Line Numbers
  1. foreach $file(@line)
  2. {
  3. chomp $r;                         #remove newline
  4. $r="#$file#\n";                    #add newline
  5. print(OUT "$r");
  6. }
  7.  
  8.  
Jan 15 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.