473,396 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STDIN - Getting Input without Return Characters

I have just started learning perl. I have to write a script that will ask a persons info, ask if you want to add more records, and once it is done print all the info with a : between the fields and a blank space in between records. Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open(FILE, ">>file.txt");
  3. while (1) {
  4.     print("First Name ");
  5.     $first = <STDIN>;
  6.  
  7.     print("Last Name ");
  8.     $last = <STDIN>;
  9.  
  10.     print FILE "$first, $last\n";
  11.  
  12.     printf(Add another record? ");
  13.     chomp($answer = <STDIN>);
  14.     last if ($answer eq "n" || $answer eq "N");
  15. }
  16. close(FILE);
  17.  
The problem I have is that is saves the $first and $last on seperate lines instead of putting them on the same line with a : between them
Mar 31 '07 #1
3 5869
Hi there,

You need to chomp each input to remove the line break character entered into the script when the user presses the return/enter key after typing in each variable, just like you do later on.

Expand|Select|Wrap|Line Numbers
  1. chomp ($first= <STDIN>);
  2. chomp ($last = <STDIN>);
  3.  
Also, it is useful to use tab characters to visually show which bits of your code are in a function or loop (are they the right terms? still new to this):

Expand|Select|Wrap|Line Numbers
  1. my $counter = 0;
  2.  
  3. while ($counter < 10) {
  4.  
  5.     # do some stuff
  6.     $counter++;
  7.  
  8. }
  9.  
Trust me, even from my very limited Perl experience this is very useful later on.
Mar 31 '07 #2
KevinADC
4,059 Expert 2GB
All you need do is chomp() the input like david has explained.
Mar 31 '07 #3
Thank you!
Mar 31 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
3
by: Fernando Arbeiza | last post by:
Hi: I need some clarification about a code like this: printf("%s", "a string with NO trailing newline"); scanf("%d", &i); Regarding if a fflush() of the standard output is needed or not. I...
4
by: Johnathan Doe | last post by:
I'm trying to find a way to reliably peek at stdin, and if anything's waiting, flush stdin so that it clears the stream ready to wait for a character. The problem I have is that in an...
6
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
11
by: Darklight | last post by:
is this correct or incorrect i just read the post below before posting this: In fflush(stdin), what happens to flushed data? this program is taken from sams teach yourself c in 21 days /*...
1
by: Luke Wu | last post by:
I have been using the getline function from K&R2 for simple character input. The prototype is int getline(char *s, int limit); /* returns 0 to signal EOF */ This function simply calls...
9
by: kernelxu | last post by:
hi, everyone. now, I'am confused on such a problem: function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0) can set the stadard input stream unbuffered. however, why does my program...
209
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.