in reply to Sub Routine Problem
People often try to overthink a problem. Since you're new to programming, you probably don't fully appreciate that simplification gives clarity. Use as few logical steps as necessary, and remove any clutter as you go along. Here's some tips:
sub card { my @card_map = qw(zero one two three four five six seven eight nine); # these aren't used, so they should be removed my $guess; my $i = 0; # there is no need for these vars to be global: our $num; our $negative = "negative"; # declare them as lexicals instead my $num; my $negative = "negative"; # declaring $num as lexical you don't need to localize: local($num) = @_; # this works here, but do you really understand what its doing? # hint: list context my ($num) = @_; # this is better my $num = $_[0]: # or this: my $num = shift @_; # commonly shortened (@_ is implicit) to: my $num = shift; # for the rest, notice how much simpler the logic # is in moritz reply above # so putting it all together: sub card{ my $num = shift; my $negative = "negative"; my @card_map = qw(zero one two three four five six seven eight nin +e); my $return_value; if ($num < 0){ $num = -$num; $return_value = $negative; } $return_value .= "$card_map[$num]"; return $return_value; } # and simplified further: sub card{ my $num = shift; my @card_map = qw(zero one two three four five six seven eight nin +e); if ($num < 0){ $num = -$num; return "negative $card_map[$num]"; } return $card_map[$num]; }
Update: corrected typo
|
|---|