rammohan has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to eliminate the empty lines while giving user input from terminal
  • Download Code

Replies are listed 'Best First'.
Re: How to eliminate the empty lines while giving user input from terminal
by kcott (Archbishop) on Feb 03, 2014 at 06:13 UTC
Re: How to eliminate the empty lines while giving user input from terminal
by davido (Cardinal) on Feb 03, 2014 at 06:37 UTC

    Ok, I've counted to 100 to let my first instincts become less likely to come out in this post..... so here goes.

    You have to decide whether you want to iterate over a file line by line, or slurp the whole thing into an array. If you try to do both, you'll probably end up with a big bug, which is what you have. So, remove "my@line = <STDIN>;". Then, print to $FH from $_, not from @line, since you've now removed the line of code that declares that variable and slurps into it. Then shift the line where you close $FH to after the loop, not inside its body. Doing those things should clear up the bugs that are really obvious. I'm unclear on what you actually want your code to do, but the first step is to at least rid yourself of the bugs that no code should ever do. ;)

    As for how to eliminate empty lines, well, that's pretty simple. Since your new code that has applied the fixes I described is now iterating line by line over the file, just look for empty lines, and don't "print" to your output filehandle when such lines are detected. There are several ways to accomplish this. You could chomp your input, and then use length to detect empty lines, or you could use a regular expression to detect empty lines. But whatever method you use for detecting empty lines, the logic will be the same; if an empty line is detected, do not print it back out to your output filehandle.


    Dave

      Then, print to $FH from $_, not from @line
      
      Given the nature of rammohan's previous posts, I'd suggest that he avoid implicit contexts($_, @_) entirely until he gains further understanding of perl.

Re: How to eliminate the empty lines while giving user input from terminal
by AnomalousMonk (Archbishop) on Feb 03, 2014 at 07:34 UTC
    open(my$FHw, '>','/home/Admin/Desktop/ram.txt') or die '$!';

    This problem is fairly trivial in the context of the others that have been pointed out, but if this open operation ever fails, the literal characters  $! are all that will ever be printed because  '$!' is a non-interpolating string. See Quote and Quote-like Operators in perlop.

Re: How to eliminate the empty lines while giving user input from terminal
by rammohan (Acolyte) on Feb 03, 2014 at 07:19 UTC
    thank you! your suggestion works fine