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

Something in my code is inserting invisible newlines into my strings in a terminal, and I would like to have any suggestions as to how to fix my code to get rid of them, or where to find such info.

code causing newline (newline 'appears' directly after $index):

say "Do you want to simplify a radical (option 1), find a prime facto +rization (option 2), or both (option 3)?\n"; $option=<STDIN>; say "Enter a number:\n"; $input = <STDIN>; if ($option==1 || $option==3){ say "Enter index of radical\n"; $index=<STDIN>; } $safe = $input; $current =$safe; #... if($current!=1){ say "$simple rad[$index] $current"; } if ($option>1){ say "The prime factorization of $safe is:\n"; say %powers;} if ($option!=2){ $simple = ($safe/$current); $current=$safe/($simple**$index); say "The simplified radical is:\n"; if($current!=1){ say "$simple rad[$index] $current"; }else{ say "$simple rad[$index]"; }}

Replies are listed 'Best First'.
Re: 'Invisible' newlines/line wrapping
by GrandFather (Saint) on Apr 23, 2010 at 01:50 UTC

    Now that you have provided a stealth update to your node to include more of the code the answer is obvious - you need to chomp $index and any other $variables you get populate using <STDIN>.

    For future reference you should note that it is polite to indicate that you have made a significant update to your node contents. People get grumpy when their reply is made to look stupid because it doesn't match the contents of the node they are replying to.

    True laziness is hard work
Re: 'Invisible' newlines/line wrapping
by GrandFather (Saint) on Apr 23, 2010 at 01:42 UTC

    Show us some code that we can run and the demonstrates the issue. There is nothing in the code you show that would generate the effect you describe unless the contents of $index is not what you expect it to be. Is it possible that you read the contents of $index from a file and it still has a trailing new line for example? A common error is to do something like:

    while (<$inFile>) { my ($this, $that, $theOther, $index) = split; }

    and miss chomping before the split.

    True laziness is hard work
Re: 'Invisible' newlines/line wrapping
by ikegami (Patriarch) on Apr 23, 2010 at 01:42 UTC

    Something in my code is inserting invisible newlines into my strings in a terminal, and I would like to have any suggestions as to how to fix my code to get rid of them, or where to find such info.

    code causing newline (newline 'appears' directly after $index):

    if($current!=1){ say "$simple rad[$index] $current"; }

    The much more plausible answer is that the value in $index ends with a newline. What's the output of

    use Data::Dumper; local $Data::Dumper::Useqq = 1; print(Dumper($index));
      it prints the inputted value for $input and then "\n"
        $index=<STDIN>;

        should be

        chomp( $index=<STDIN> );

        You never removed the newline Enter produced.

Re: 'Invisible' newlines/line wrapping
by ww (Archbishop) on Apr 23, 2010 at 01:46 UTC

    Update: OP updated as this was written. Original (or maybe second version of 3?) was actually better. In any case, my refs to lines 2 and 5 should now be read as 5 and 16&27 (I think).</update>

    Your input, $index=<STDIN>; at line 2 is going to have a trailing newline (think of the return you hit to terminate the input, if that notion is helpful).

    So, to have line 5 print without an inadvertent newline, make line 3:
    chomp $index;
    ... and your problem should be solved.

    Now, we know this is just a snippet, but FTR, one hopes that you're using strict and warnings which will actually help you avoid or understand many other apparently odd behaviors.

    And, welcome to the Monastery!

Re: 'Invisible' newlines/line wrapping
by gooshie (Initiate) on Apr 23, 2010 at 01:48 UTC
    chomp($index=<STDIN>);
Re: 'Invisible' newlines/line wrapping
by luolimao (Initiate) on Apr 23, 2010 at 01:51 UTC
    Thank you so much! And I would append -w to #!perl (or on UNIX systems, the line with #!/usr/bin/perl) to print the warnings, right?

      Always use strictures (use strict; use warnings). Not just warnings (which you sort of get with -w), but strict too.

      -w applies to the entire script which will affect modules too. use warnings only applies to the enclosing block (normally a file) and is generally most appropriate.

      True laziness is hard work
      Not exactly.

      A "-w" on the shebang line has slightly different effects that "use warnings;" on a following line.

      You deserve a reference to read about those differences; sorry; it's waaaay past bedtime, so perhaps another Monk will provide one. Meantime, try searching for "pragmata."

        You deserve a reference to read about those differences
        From warnings:
        The warnings pragma is a replacement for the command line flag -w , but the pragma is limited to the enclosing block, while the flag is global. See perllexwarn for more information.
        Also: perllexwarn