LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   New to perl programming (https://www.linuxquestions.org/questions/programming-9/new-to-perl-programming-302648/)

jester_69 03-17-2005 03:51 AM

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?

sleetdrop 03-17-2005 04:27 AM

#!/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.

TheLinuxDuck 03-17-2005 02:45 PM

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.


All times are GMT -5. The time now is 08:46 PM.