learning examples

S

Sam

I am following an online study to learn Perl.
I wrote my first 2 programs and run them but unable to find what's wrong
with them.
the first is as bellow it prints a "\n" in the output right after the $nmber
so that converts to ... is on a new line. why?

#!/usr/bin/perl -w

use strict;

print "please type a hexadecimal number\n";

print "it should starts with 0x and ends with a character less than F: ";

my $nmber= <STDIN>;

print "\$$nmber converts to decimal number: ", hex("$nmber"), "\n";
 
M

Matt Garrish

the first is as bellow it prints a "\n" in the output right after the $nmber
so that converts to ... is on a new line. why?

my $nmber= <STDIN>;

print "\$$nmber converts to decimal number: ", hex("$nmber"), "\n";

When you read from stdin, you are capturing the "\n" the user enters to
indicate that input is finished (i.e., when you press the enter key). If you
don't want it in your string, chomp it:

my $nmber=<STDIN>;
chomp $nmber

Matt
 
J

John W. Krahn

Sam said:
I am following an online study to learn Perl.
I wrote my first 2 programs and run them but unable to find what's wrong
with them.
the first is as bellow it prints a "\n" in the output right after the $nmber
so that converts to ... is on a new line. why?

#!/usr/bin/perl -w

use strict;

print "please type a hexadecimal number\n";

print "it should starts with 0x and ends with a character less than F: ";
^^^^^^^^^^^^^^^^^^^^^^^^
This is not true. '0x' is not required for hex() to work.

$ perl -le' print "$_ " . hex( $_ ) for qw( abcd 0xabcd ) '
abcd 43981
0xabcd 43981

my $nmber= <STDIN>;

Input from STDIN will have a newline at the end of every line. You have
to remove it if you don't want it there.

chomp $nmber;

print "\$$nmber converts to decimal number: ", hex("$nmber"), "\n";
^ ^
perldoc -q quoting



John
 
T

Tad McClellan

David Oswald said:
a copy of Learning
Perl, published by O'Rilley & Assoc, written by Tom Christiansen and Randal
Schwartz.


Don't get that old 2nd edition, get the newer 3rd edition
by Randal and Tom Phoenix.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top