CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
User input

 



Sindbad
New User

Apr 4, 2013, 6:37 PM

Post #1 of 3 (395 views)
User input Can't Post

Hello there
I have s very simple Perl question
How can I get a user input and write is to a file
Say, I want to ask the user for 10 sentence of input
I then want to write these 10 sentence to a file, let us name it input.txt
I and thinking like this
Print "Please enter 10 senteces.\n";
S_ =(<STDIN>);
Then what?
As you see I am still scratching the surface of Perl world
Any help would be appreciated


Kenosis
User

Apr 4, 2013, 8:24 PM

Post #2 of 3 (389 views)
Re: [Sindbad] User input [In reply to] Can't Post

Here's one way:


Code
use strict; 
use warnings;

my @sentences;
print "Please enter 10 sentences.\n";

for ( 1 .. 10 ) {
print "\nSentence $_ (press <Enter> when done):\n";
my $sentence = <>;
push @sentences, $sentence;
}

open my $fh, '>', 'input.txt' or die $!;
print $fh $_ for @sentences;
close $fh;

print "\nThank you.\n";


This first prompts the user for the 10 sentences, then enters a for loop that iterates 10 times, getting a sentence each pass and pushes those sentences onto an array. The array's elements are then written out to a file.

Hope this helps!


Sindbad
New User

Apr 5, 2013, 7:25 AM

Post #3 of 3 (380 views)
Re: [Kenosis] User input [In reply to] Can't Post

Hello

This is just great, I would not ask for more, good example to learn form, all appreciated


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives