Problem with a string

Discussion in 'Perl Misc' started by Default@User.net, Nov 5, 2003.

  1. Guest

    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);
    }
     
    , Nov 5, 2003
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Nov 5, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    739
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,613
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    509
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,931
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,803
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page