LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2005, 03:51 AM   #1
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Rep: Reputation: 15
Question New to perl programming


Hi all,

I hope you can help. I am new to the world of programming & am finding it frustrating getting my head around its logic. I have a few problems a teacher has given me & am neeing to see how different people would create/solve them?

For example

1./ Construct an algorithm that will prompt a user at a keyboard to imput 3 seperate characters ( that is, press the <enter> key three times), receive those 3 characters, and display a welcom message to the screen like Hello $name! I hope your having a good day.

I have done it like this

#! /usr/bin/perl

$name = "Andrew"
$count = 3
print "Hello '$name'\"n;
print "We hope you have a nice day"\n

How would i imput the 3 key presses into this code.

I know the count function cannot be correct.

Do you guys mind if i throw some more at you?
 
Old 03-17-2005, 04:27 AM   #2
sleetdrop
LQ Newbie
 
Registered: Mar 2005
Posts: 16

Rep: Reputation: 0
#!/usr/bin/perl
print "What's your first name?\n";
chomp($firstname=<STDIN>);
print "What's your last name?\n";
chomp($lastname=<STDIN>);
print "How old are you?\n";
chomp($age=<STDIN>);
print "Nice to meet you, $firstname $lastname. A $age years old guy!!!\n";


I am a Chinese, I am not very good at english.
I don't know whether the code up will helpful to you.
The book "Learing Perl" from O'REILLY is not bad for a good start.
I am a newbie, too.
 
Old 03-17-2005, 02:45 PM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Jester:

Are you new to coding in general or just perl?

When I first took on perl, I was overwhelmed, but it does get easier.

A note in input, as given in the example by sleetdrop.

If you are familiar with linux or any other coding, you will no doubt be familiar with stdin and stdout, as these are common streams. These streams work as though they were a file handle, which means that you can read from them and write to them just like you would a file.

The example uses <STDIN>. The <> can be considered 'slurp' operators. Whatever file handle you put between them is going to be slurped up and returned. For example, to load an entire file into an array, you could say:
Code:
#!/usr/bin/perl

my($file) = '/etc/mail/sendmail.cf';

open IN, $file or die "Cannot open file: $!\n";
  #  Now, we slurp the file into an array variable
  my(@content) = <IN>;
close IN;
If any of this doesn't make sense, don't worry about it just yet, just look at the <> line.

And since STDIN is basically the same as a file handle, you slurp into it the same way, just like in sleetdrop's example.

I hope that this helps and makes sense to you! If you have any questions, please ask. I will do my best to answer.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Programming! emailssent Programming 3 06-05-2005 02:25 AM
Programming in Perl mimf Programming 5 12-04-2003 04:58 AM
Need help with perl (cgi) programming elmetald00d Programming 4 03-28-2002 11:15 PM
Perl programming question LindsayJ Programming 0 11-11-2001 02:58 PM
Perl programming AliceC Programming 4 05-17-2001 10:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration