On Tue, Oct 14, 2008 at 02:15, Richard Lee <[EMAIL PROTECTED]> wrote:
> below sub works fine except the line where key is default.
> Instead of printing out PCMU only once, it's printing it out 40 times
> randomly..
> Trying to figure out what I did wrong.
snip

I found your code to be very odd.  You do not normally loop over the
keys in a hash to determine if there is a match; you just ask the hash
if a match occurred.  Did your code start out using the arrays at the
top instead of a hash?  I have rewritten your function to make better
use of the hash and provide error handling for out of range requests.
The error was in your looping code (the default case was being
executed for every key in the hash).

#!/usr/bin/perl

use strict;
use warnings;

$|=1;

for my $request (qw/default A B C D/, 0 .. 126) {
    print codec_list([$request]), "\n",
}

print join(", ", codec_list([0 .. 126])), "\n";

codec_list([-1, 128]);

sub codec_list {
   my $number = shift;

   #FIXME: this should really be in a file
   my @codec_d = qw/0 default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 A B C D/;
   my @codec_i = qw/PCMU PCMU Reserved Reserved GSM G723 DVI4 DVI4 LPC
PCMA G722 L16 L16 QCELP CN MAP G728 DVI4 DVI4 G729 reserved unassigned
unassigned unassigned unassigned unassigned CelB JPEG unassigned nv
unassigned unassigned H261 MPV MP2T H263 unassigned reserved
unassigned dynamic/;
   my %codec_r;
   @[EMAIL PROTECTED]  = (@codec_i);
   $codec_r{$_} = "$codec_r{A}_$_" for 35 .. 71;
   @codec_r{72 .. 76}  = ($codec_r{B}) x ( 77 - 72);
   @codec_r{77 .. 95}  = ($codec_r{C}) x ( 96 - 77);
   @codec_r{96 .. 126} = ($codec_r{D}) x (127 - 96);

   my $errors = 0;
   my @return_codec;
   for my $request_key ( @$number ) {
       if (exists $codec_r{$request_key}) {
           push @return_codec, $codec_r{$request_key};
           next;
       } else {
           warn "$request_key is not valid";
           $errors++;
       }
   }
   die "had $errors errors" if $errors;
   return @return_codec;
}



--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to