Translate characters into variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fifou92
    New Member
    • Jul 2008
    • 1

    #1

    Translate characters into variable

    Hi,

    I would like to translate (Encrypte) a code from a variable.
    This code is made of 3 numbers and substitution must respect a rule according number position.
    For exemple:
    If number 1 is the first position in the code, it must be translated to A
    If number 1 is the second position in the code, it must be translated to B
    If number 1 is the third position in the code, it must be translated to C

    If number 0 is the first position in the code, it must be translated to Z
    If number 0 is the second position in the code, it must be translated to Y
    If number 0 is the third position in the code, it must be translated to X

    If number 2 is the first position in the code, it must be translated to M
    If number 2 is the second position in the code, it must be translated to N
    If number 2 is the third position in the code, it must be translated to P

    If the code is 102, translated code will be AYP

    I have written following code but it doesn't run.

    Code:
    my $EncodedLockJob=0;
    
    my LockJob0="AZM";
    my LockJob1="BYN";
    my LockJob2="CXP";
    
    my $LockJobLength=3;
    
    my LockJobCharIndex;
    my LockJobChar;
    my LockJobN;
    for ($LockJobCharIndex = 0; $LockJobCharIndex < $LockJobLength; $i++) {
    $LockJobChar = substr(102, LockJobCharIndex, 1);
    if ($LockJobChar == "0")
    { $LockJobN = $LockJob0; }
    elsif ($LockJobChar == "1")
    { $LockJobN = $LockJob1; }
    else ($LockJobChar == "2")
    { $LockJobN = $LockJob2; }
    $EncodedLockJob = $EncodedLockJob + substr($LockJobN, $LockJobCharIndex, 1);

    Thanks for your help
    Last edited by eWish; Jul 1 '08, 11:38 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    You need a '$' in front of your scalar variables, like here:

    Code:
    my LockJob0="AZM";
    my LockJob1="BYN";
    my LockJob2="CXP";
    should be:

    Code:
    my $LockJob0="AZM";
    my $LockJob1="BYN";
    my $LockJob2="CXP";
    fix all of those errors and retry your code.

    Comment

    Working...