Simple IO program test

1 view
Skip to first unread message

chikega

unread,
Jul 14, 2022, 12:03:58 AM7/14/22
to Pike mailinglist
Hello everyone,
I started this email because I initially had a question, but I was able to figure it out. I'm learning PIke and for practice I have written this simple IO program in half a dozen plus languages especially C-based languages, D being one of my favorites, as seen here:


import std.stdio;
import std.string; // strip(), toLower();
import core.thread; // sleep();

void main() {
string userInput;
do {
write("> ");
userInput = readln.strip();
writeln("You wrote '", userInput, "'");
} while (toLower(userInput) != "q");
writeln("Goodbye!");
Thread.sleep(2.seconds);
}


The program outputs whatever the user inputs and exits when the user inputs "q" or 'Q'. It's always interesting to see how various programs differ. For example, some languages will capture the newline char in the IO stream and strip or trim functions are needed, not so in Pike. Although I'm a beginner-to-intermediate programmer, it didn't take me long to figure out the Pike syntax and I find it to be very pleasant to code in. Also I'm testing if formatted code is visible within this mailing list - if not, then the attached image shows partially what I'm seeing on my end. :) 


int main() {
string userInput;
do {
write("> ");
userInput = Stdio.stdin-> gets();
write("You wrote '" +userInput+ "'\n");
} while (lower_case(userInput) != "q");
write("Goodbye!\n");
sleep(2); // 2 seconds
return 0;
}


Thank you, 
Gary

Screen Shot 2022-07-14 at 12.01.12 AM.png

William Welliver

unread,
Jul 14, 2022, 11:48:34 AM7/14/22
to chikega, Pike mailinglist
Hi Gary, and welcome!

I’ve always felt that D would have been “my” language if I hadn’t found pike first, all those years ago. The two seem to share a good deal of spiritual commonality. 

Something that’s been said about pike over the years is that it tries to produce the best (fastest, does what you want) result when you write the most straightforward code to implement it. That’s certainly not true of all languages (I’m looking at you, Java!)

For extra credit, you might look at the Readline module, which has some nice features for reading input. Sadly the documentation for it is poor to nonexistent… though I can point to examples of it in use if anyone is interested. Maybe I’ll even find some time to document the bits I understand if I get a few free moments!

Anyhow, welcome again and don’t hesitate to post a message if you have any questions in the future. We’re always happy to help!

On Jul 14, 2022, at 12:04 AM, chikega <chi...@gmail.com> wrote:



chikega

unread,
Jul 17, 2022, 1:59:39 PM7/17/22
to William Welliver, Pike mailinglist
Hi William, 
I really gave it a go trying to figure out how to use Stdio.Readline using every language syntax permutation that I'm familiar with such as D and Object Pascal(readln), C#: (Console.ReadLine), etc.. 

The documentation as you've mentioned appears to be incomplete concerning Readline(). Well,  I even copied this entire program with the use of readline, but I just get a bunch of errors. So yes, it would be really cool if you could point me in the right direction :) 



Reply all
Reply to author
Forward
0 new messages