My Dearest Monks. A while ago, I had written my first sub routine, had some malfunctions, asked for your help, learned some valuable lessons and have since moved on. Well, I am now trying to expand that same routine, and it isn't working correctly, and lo and behold...I need your expertise yet again. This is the sub routine:

#!usr/bin/perl use strict; my $guess; print "Enter your number...\n"; chomp($guess = <STDIN>); print "card of $guess is ", &card($guess), "\n"; print "\nRockstar Programming Inc. All Rights Reserved\n"; ## subroutines ### sub card { my @card_map = qw(zero one two three four five six seven eight nine); my $guess; my $i = 0; our $num; our $negative = "negative"; local($num) = @_; if ($card_map[$num]) { $card_map[$num]; # return value } else { $num; # return value } # end if if ($num > 0) { $num = - $num; } # end if if ($card_map[$num]) { $negative . $card_map[$num]; # return value } else { $negative . $num; # return value } # end if } # end sub

The code works, however my output isn't what it should be. I am looking to get the cardinal name returned for each number I enter from 1 - 9, or from -9 - 0, and the same number returned for all other entries. However, the words given in my output, are not matching the numbers entered. Theoretically it should work. I think the problem is in how the input is being matched against the @, because the output is off, but it looks to me as though it shouldn't be doing this?

Any suggestions would be greatly appreciated. Also, as always, please forgive my simplicity, as I am very brand new to the programming world, and I realize my code must be elementary and border on a waste of time for most. I have spent quite a bit of time, reviewing every technique I'm using in this code, in both my llama book, the camel book and on-line docs, and I even have an almost exact example of Randall's, which I tweaked (which is basically what this mostly consists of - tweaked examples of Randall's from the llama book), and all of these say this should work. Any clue?

P.S. Thank you for your time Monks.

UPDATE: Fixed a "typo", thanks Lawliet ;)


In reply to Sub Routine Problem by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.