[svlug] It's true!

Dan Martinez dfm at area.com
Wed Aug 23 09:26:02 PDT 2000


Following up on my own post:

Rich Bodo wrote:

> I was about 75% of the way done translating the decoding-to-UPC
> portion of the PHP code to a simple perl script when I ran into this
> PHP function: strtr(). It converts a string based on a hash table.
> Does anyone know of an equivalent Perl function? I couldn't think of
> anything off hand.

I've looked at the PHP source now, and Perl's hash lookup is almost
certainly what you want. (Conveniently, PHP uses a syntax almost
identical to Perl's for populating hashes, so you can crib the
original hash almost verbatim.) This should do it:

my %code = (
    "C3" => 0,
    "n"  => 0,
    "Z"  => 0,
    "L"  => "Z",
    "G"  => 0,
    "CN" => 1,
    "CW" => 0,
    "CH" => 1,
    "j"  => 1,
    "Y"  => 1,
    "Cx" => 2,
    "f"  => 2,
    "X"  => 2, 
    "Ch" => 3,
    "b"  => 3,
    "W"  => 3,  
    "D"  => 4,
    "3"  => 4,  
    "D3" => 4,
    "DN" => 5,
    "z"  => 5,
    "2"  => 5,
    "Dx" => 6,
    "v"  => 6,
    "1"  => 6,  
    "u"  => 6,
    "Dh" => 7,
    "r"  => 7,
    "0"  => 7,  
    "q"  => 7,
    "E3" => 8,
    "T"  => 8,
    "7"  => 8,  
    "EN" => 9,
    "P"  => 9,
    "6"  => 9  
);

Later, you'd perform the actual lookup with:

$temp = $code{$temp2};

Mind you, I haven't tested any of this.

Dan




More information about the svlug mailing list