Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Noob's question

by gonny95 (Initiate)
on Sep 27, 2014 at 10:58 UTC ( [id://1102207]=perlquestion: print w/replies, xml ) Need Help??

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

my $s=""; while($s.=<STDIN>){print $s;}
This code doesn't work as I expected. It should concat keywords I input but It doesnt

Replies are listed 'Best First'.
Re: Noob's question
by trizen (Hermit) on Sep 27, 2014 at 11:18 UTC

    There is a newline character at the end of each line after you press ENTER.
    To remove this unwanted character, Perl has the chomp function which does the trick.

    my $s=""; while(chomp($s.=<STDIN>)){print "$s\n"}
Re: Noob's question
by ww (Archbishop) on Sep 27, 2014 at 11:34 UTC
    We really need a bit more information: HOW "doesn't (it) work as (you) expected?
    1. Spew warnings; spit errors.... or what?
    2. and what OS is this running under?
    3. and HOW are you entering the input -- as a set of words on the CLI invoking the code, properly quoted; as individual words once the code is running; by telephathic instantiation?
    See, among other FAQs about how to ask (and title) a question How do I post a question effectively? and I know what I mean. Why don't you?.

    BTW, your code works as expected, when used to process input in the manner you've specified with <STDIN>... which is not the same way as it would work were you trying to process @ARGV.


    ++$anecdote ne $data


      Sorry I saw the output wrong. Actually It seems to work fine. And next time I will write more detailed question thanks!

        Sorry I saw the output wrong. Actually It seems to work fine.

        I find it highly unlikely that your code works, considering it's very likely to go into an infinite loop...

Re: Noob's question
by Don Coyote (Hermit) on Sep 27, 2014 at 12:18 UTC

    I agree with ww, it does work as expected, you are concatenating the newline character. trizen suggests a solution to remove that character. However I think the issue here is how to exit the while loop. If you assign and concatenate you attach the exit keystroke onto the variable you are processing. Mixing your control with your data.

    There are also modules such as IO::Prompter which can handle a lot of this for you.
    #!/usr/bin/perl -wl use strict; my $s; my $chomp = 1; # actually I do want to remove the newline; while(<STDIN>){ chomp if $chomp; unless( /^q$/i ){ # exit from inputting is 'q' or 'Q' $s .= $_; # concatenate print $s; }else{ last } } print $s; exit 0;

    nice trick though, hadn't thought of using operators other than simple assignment

Re: Noob's question
by Anonymous Monk on Sep 27, 2014 at 12:19 UTC

    As soon as $s contains a "true" value, your loop will not terminate because the loop condition will always be true.

    trizen's suggestion doesn't do that because chomp returns the number of characters removed, which will be zero when end-of-file is reached on STDIN - but it won't terminate on a blank line, since that still has a newline character. Personally, I might have written it like this:

    while(<STDIN>) { chomp; $s.=$_; print "$s\n"; }

    However, putting the reading of STDIN in the condition of the while makes me suspect that you want to terminate the loop when an empty line is input. If that's what you want, here's one way to do that:

    while(<STDIN>) { chomp; last unless length; $s.=$_; print "$s\n"; }

    Also, often, using <> instead of <STDIN> can be helpful because it gives you the power of Perl's magic ARGV (see I/O Operators). Unless you explicitly want to read from STDIN, I'd suggest using <>.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102207]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-03-28 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found