Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with a string

Reply
Thread Tools

Problem with a string

 
 
Default@User.net
Guest
Posts: n/a
 
      11-05-2003
Hi im new to perl and programming, could you please help me with this problem?
When I enter two out of range values I get unexpected results.
eg: -20 and 20
gives this for output: Negative plus equals zero

Here is the program (sorry for my noobness).
Thank you for any help.
Sorry if this is a double post, im new to posting nntp also : |

#!
hash_one ();
print "\n" . "="x80;
print "Takes two numeric values from -9 to 9 adds them then returns the English names.";
print "\n" . "="x80 . "\n\n";
until (($key1 eq "end") || ($key2 eq "end"))
{
$neg1 = ""; # Im doing this to try to fix the problem
$neg2 = ""; #
$neg3 = ""; # o 0
$key1 = ""; # _
$key2 = ""; #
$key3 = ""; #
print "Enter a number (0-9):\t\t";
chomp ($key1 = <STDIN>);
print "Enter another number (0-9):\t";
chomp ($key2 = <STDIN>);
$key3 = ($key1 + $key2);
if ($key1 < 0)
{
$neg1 = "negative ";
$key1 = (- $key1);
}
if ($key2 < 0)
{
$neg2 = "negative ";
$key2 = (- $key2);
}
if ($key3 < 0)
{
$neg3 = "negative ";
$key3 = (- $key3);
}
lookup_one ($key3);
}
#####
sub lookup_one
{
if (($key1 eq "end") || ($key2 eq "end"))
{
print "\nEnding program\n";
last
}
if ($hash_one{$key3})
{
$output1 = $neg1 . $hash_one{$key1} . " plus " . $neg2 . $hash_one{$key2} . " equals " . $neg3 . $hash_one{$key3};
print "\n\u$output1\n\n";
}
else
{
$output2 = $neg1 . $key1 . " plus " . $neg2 . $key2 . " equals " . $neg3 . $key3;
print "\n\u$output2\n\n";
}

}
sub hash_one
{
%hash_one = qw(
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
12 twelve
13 thirteen
14 fourteen
15 fifteen
16 sixteen
17 seventeen
18 eighteen
19 nineteen);
}
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-05-2003
wrote:
> When I enter two out of range values I get unexpected results.
> eg: -20 and 20
> gives this for output: Negative plus equals zero


Unexpected? Why? What had you expected it to print?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments