K guys,
I'm I am calling the day off. I will catch you surely tomorrow. Hope you will debug your code. Happy posting :) |
Quote:
Code:
while(<>){ |
Hi,
@ghostdog74: One of the things the OP does not comprehend is the way STDIN works and how to implement this in a perl program. He will keep complaining about "being stuck in a loop", which is also "true" for your short and elegant solution. But without a clear description of what it is he tries to accomplish (is perl a must, is there always 1 line of input from STDIN or could there be more, is STDIN actually needed) solving his problem for him (it looks like learning is not on his agenda) is going to be problematic. |
Quote:
|
Quote:
This is your duty to verify the while loop condition. You shouldn't have just taken the code published by others (an I only published a statement supposed to help debugging), you should have analyzed the root cause and should have corrected it. This is not your first thread regarding your Perl problems, and to me it looks like you repeat making the same mistakes. This is you who gave no explanation regarding Quote:
|
Quote:
Code:
echo "hi there hi hi" | perl myscript.pl |
Hi ghostdog74,
Quote:
Quote:
Quote:
Also, I request all you guys to suggest me documents in learning perl effectively. |
Quote:
Code:
while($s ne '') |
Quote:
Could you please check your code with the input as "hiiiiiii"? |
Hi ashok.g,
Let's try to get this back on track again....... I'm curious about something: Have you played with and do you understand the following example (I used ghostdog74's example [post #17], because it is the most elegant and simple): Code:
#!/usr/bin/perl You mention the following in one of your replies: Quote:
A carriage return (enter or return if you will, also represented in linux as \n or \r) is not an EOF, it is seen as part of the input. Using chomp will not help. Chomp takes off the end character of a specified string ONLY if that character is a RETURN (Enter). If you do the following: echo "not getting hi here" | ./hi.counter.pl (hi.counter.pl being the example I posted a few lines back): the output will be: count: 1 And you will have your prompt back. If you do this: ./hi.counter.pl And enter not getting hi here from the keyboard, you will get count: 1 but you will not get your prompt back. The first one also echo's an EOF and the script (be it perl, bash, awk or whatever) knows there is no more input coming from STDIN and it can continue. The second example does not echo an EOF and STDIN keeps waiting for input. You can enter an EOF from the keyboard by pressing ctrl-d. This will tell STDIN that no more input is comming and the script can continue (STDIN can take multiple lines). STDIN, STDERR and STDOUT are not perl specific. A few URL's about this subject: (perl) Reading Input from Standard Input (wiki) Standard streams (shell) stdin, stdout, stderr Hope this finally clears things up a bit. |
Hi druuna,
Thanks for your post #25. I really understood the program which ghostdog74 quoted in the post #17 with the concept I have about EOF, EOL and STDIN. But your explanation for them is very neat and clean. :) Coming back to the line which I had written in the code, Code:
while ( $f ne "" ) Code:
while ( $f ne '') In this line we are not expecting any EOL character rather than we are expecting the condition to be failed. Isn't it? But as you said we are not echoing any EOL using <STDIN>, then it must wait for the input( You also specified this point in your reply :"The second example does not echo an EOL and STDIN keeps waiting for input") rather than going for an infinite loop( please give any prrint statement like print ("------\n")inside while loop then you can see its goiing infinite loop). |
Hi,
Stupid me, I mixed up EOL and EOF in my previous reply (already fixed it). Please have another look. Sorry for that!! |
But what about my post, #26
|
Hi again,
The reason I used ghostdog74 example and not your initial script is 'cause your script is basically flawed. This $a = <STDIN> ; will set up the STDIN 'rules' (waiting for a EOF, which is essentially a loop). After that you create another loop (the while ( $f ne '' ) { part), within the first loop. The action part of the while construct (if ....) is caught between 2 loops. You have a valid script if you re-write your original script to this: Code:
#!/usr/bin/perl echo "not getting hi here" | ./hi.counter.2.pl Output: The total no. of ocurrances are 1 And you have your prompt back. Or: ./hi.counter.2.pl Enter text from keyboard: not getting hi here (with or without enter/return [=EOL!!]) Enter EOF char to stop the loop: ctrl-d Output will be: The total no. of ocurrances are 1 or not getting hi hereThe total no. of ocurrances are 1 depending on the enter/return you did (output 1) or did not (output 2) press. In both cases your prompt is back. Hope this helps. |
I'm really exhausted today with my work and these posts.
Druuna, Your last code is working fine with respective to the control loops and all other stuff but it actually doesn't serving the main functionality, i.e., counting the no.of occurances of a word. (please give multiple "hi"s in multiple lines like hi hi hi hiiiiiiiiiiiii hhhhhhhhhhi ) It's giving the worng output even after I gave /g in matching there. |
All times are GMT -5. The time now is 12:08 AM. |