koolgirl has asked for the wisdom of the Perl Monks concerning the following question:
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 ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sub Routine Problem
by moritz (Cardinal) on Oct 23, 2008 at 07:11 UTC | |
|
Re: Sub Routine Problem
by andreas1234567 (Vicar) on Oct 23, 2008 at 07:26 UTC | |
|
Re: Sub Routine Problem
by hangon (Deacon) on Oct 23, 2008 at 14:02 UTC | |
|
Re: Sub Routine Problem
by Limbic~Region (Chancellor) on Oct 23, 2008 at 14:03 UTC | |
|
Re: Sub Routine Problem
by scorpio17 (Canon) on Oct 23, 2008 at 14:18 UTC | |
|
Re: Sub Routine Problem
by apl (Monsignor) on Oct 23, 2008 at 10:37 UTC | |
by Zen (Deacon) on Oct 23, 2008 at 16:09 UTC | |
|
Re: Sub Routine Problem
by blazar (Canon) on Oct 23, 2008 at 19:15 UTC | |
|
Re: Sub Routine Problem
by Utilitarian (Vicar) on Oct 23, 2008 at 14:02 UTC | |
|
Re: Sub Routine Problem
by koolgirl (Hermit) on Oct 24, 2008 at 06:20 UTC | |
by Limbic~Region (Chancellor) on Oct 24, 2008 at 18:02 UTC |