Current location: Hot Scripts Forums » Programming Languages » Perl » how to exit from keyboard input??


how to exit from keyboard input??

Reply
  #1 (permalink)  
Old 05-01-04, 11:19 PM
chaos7 chaos7 is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
how to exit from keyboard input??

Hello,
I've got a program which takes in lines of text via keyboard input, but i am not sure how to place in code to end input by placing a full stop on a newline? Can anyone help?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-02-04, 06:03 AM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by chaos7
Hello,
I've got a program which takes in lines of text via keyboard input, but i am not sure how to place in code to end input by placing a full stop on a newline? Can anyone help?
Post the code here and I'll see if I can help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-02-04, 10:46 PM
chaos7 chaos7 is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
[QUOTE=Skeleton Man]Post the code here and I'll see if I can help

hi,

basically i've got to get keyboard input either in single line mode or multiline. for example, for single line the arguments must be written next to the executable file for it to print out (stored in @ARGV)

ie.
shell> ./script cat in hat
cat in hat
shell>

but then if you just put the executable file without command line arguments it should let you type in multilines and typing a fullstop should exit program.

ie:
shell> ./script
this is
a test
.
shell>

im not sure how to yet put the above into a code??? so far i've just been working with the modes seperately, my code for the multiple lines is;

my $line;
my @ARGV=$line;
while (defined($line=<>)) {
#my $term='.';
#if ($line eq $term){
#exit;
#}else{
print " $line";
#}
}

the commented if else statement doesnt work and doesnt exit with fullstop either....???????????
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-02-04, 11:25 PM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
Try the following, it should read input line by line untill you type '.' :

Code:
$txt = '';

while ($txt ne '.'){
 chomp($txt = <STDIN>);
 if ($txt ne '.'){ $buf .= "$txt "; }
}

print "\nYou Typed: $buf\n";
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-03-04, 04:09 AM
chaos7 chaos7 is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Whoo hoo!! that works great!! your a legend!! any ideas how input can be intepreted ie, either single line mode (by giving command line argument) or multiline mode (no command line argument) as per above example??? i can't find in any book or internet resources?? but if not its okay my biggest prob was the termination with fullstop, thanks a lot
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 05-03-04, 05:10 AM
Skeleton Man Skeleton Man is offline
Community Liaison
 
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
Here's the basic structure for either single or multi-line input:

The best learning resource for perl is the O'Reilly series of books, I believe there's one with a camel or lama on the front, the title escapes me though..

Code:
#!/usr/bin/perl


# Arrays used in this context give us the number of elements.
# In this case we're using @ARGV which holds command line arguments.


if (@ARGV > 0){ 
 
 #
 # We have a least one command line argument.
 #
 
 print "\nArguments: ";
 foreach $arg (@ARGV){ print "$arg "; }
 print "\n";
 
}else{

 # 
 # No arguments were given, accept input.
 #
 
 $txt = '';

 while ($txt ne '.'){
  chomp($txt = <STDIN>);
  if ($txt ne '.'){ $buf .= "$txt "; }
 }

 print "\nYou Typed: $buf\n";
}

Last edited by Skeleton Man; 05-03-04 at 05:14 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to validate mutiple rows using the same input text Han84 JavaScript 1 08-02-07 10:14 AM
check input for invalid characters simone PHP 10 04-20-04 03:33 AM
File input daveyisu Windows .NET Programming 1 03-25-04 05:08 PM
How to enabled or disabled input text using a checkbox Han84 HTML/XHTML/XML 1 10-13-03 09:46 AM
email input fro scripts seriousfun Perl 3 06-30-03 08:41 PM


All times are GMT -5. The time now is 12:47 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.