2 basics questions: 1)'a' < 'b' 2)Run, but is it ok?

O

Olaf \El BLanco\

1) Why this not work? I need the OK when a < b, B < Z, etc...
print "First char: "; $c1 = <STDIN>;
print "Second char: "; $c2 = <STDIN>;
if ($c1 <= $c2)
{ print "OK"; }


2) print "Give me 4 numbers...\n";
print "-->"; $n1 = <STDIN>;
print "-->"; $n2 = <STDIN>;
print "-->"; $n3 = <STDIN>;
print "-->"; $n4 = <STDIN>;
$larger = $n1;
if ($n2 >= $larger) { $larger = $n2; }
if ($n3 >= $larger) { $larger = $n3; }
if ($n4 >= $larger) { $larger = $n4; }
print "The biggest is $larger";
 
A

Anno Siegel

Olaf \"El BLanco\ said:
1) Why this not work? I need the OK when a < b, B < Z, etc...
print "First char: "; $c1 = <STDIN>;
print "Second char: "; $c2 = <STDIN>;
if ($c1 <= $c2)
{ print "OK"; }

Strings are compared using "le", not "<=". See perldoc perlop.
2) print "Give me 4 numbers...\n";
print "-->"; $n1 = <STDIN>;
print "-->"; $n2 = <STDIN>;
print "-->"; $n3 = <STDIN>;
print "-->"; $n4 = <STDIN>;
$larger = $n1;
if ($n2 >= $larger) { $larger = $n2; }
if ($n3 >= $larger) { $larger = $n3; }
if ($n4 >= $larger) { $larger = $n4; }
print "The biggest is $larger";

Even trivial programs like these are best developed under strict and
warnings. Switch them on. You'll have to declare your variables
with "my" after that. Do that too.

Anno
 
B

Bart Lateur

Olaf said:
1) Why this not work? I need the OK when a < b, B < Z, etc...
print "First char: "; $c1 = <STDIN>;
print "Second char: "; $c2 = <STDIN>;
if ($c1 <= $c2)
{ print "OK"; }

A "char" is a string (with length 1), not a number. Forget all your
preconceived ideas from C, that a string is an array of numbers... it
isn't. Instead, it's far more like a string in Basic.

You compare strings with le, lt, eq, ne, ge, gt.
2) print "Give me 4 numbers...\n";
print "-->"; $n1 = <STDIN>;
print "-->"; $n2 = <STDIN>;
print "-->"; $n3 = <STDIN>;
print "-->"; $n4 = <STDIN>;
$larger = $n1;
if ($n2 >= $larger) { $larger = $n2; }
if ($n3 >= $larger) { $larger = $n3; }
if ($n4 >= $larger) { $larger = $n4; }
print "The biggest is $larger";

You could put the results in an array, and loop through them. Heck, you
could even loop through the list of scalars:

$larger = $n1;
foreach my $n ($n2, $n3, n4) {
if ($n >= $larger) { $larger = $n; }
}
print "The biggest is $larger";
 
P

Paul Lalli

Bart said:
You could put the results in an array, and loop through them. Heck, you
could even loop through the list of scalars:

$larger = $n1;
foreach my $n ($n2, $n3, n4) {
if ($n >= $larger) { $larger = $n; }
}
print "The biggest is $larger";

Or, more simply IMHO,
use List::Util qw/max/;
my $larger = max($n1, $n2, $n3, $n4);

(if you have perl 5.8, this will also be much faster, as it uses
compiled XS code rather than Perl looping)

Paul Lalli
 
R

robic0

1) Why this not work? I need the OK when a < b, B < Z, etc...
print "First char: "; $c1 = <STDIN>;
print "Second char: "; $c2 = <STDIN>;
if ($c1 <= $c2)
{ print "OK"; }
This works for numbers and chars, but only checks the first character
use strict;
use warnings;

print "First char: "; my $c1 = <STDIN>;
print "Second char: "; my $c2 = <STDIN>;
if (ord($c1) <= ord($c2))
{ print "OK"; }
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top