Link to home
Start Free TrialLog in
Avatar of BrianGEFF719
BrianGEFF719Flag for United States of America

asked on

convert VB to PERL

Hi, i need the following visual basic function converted to perl. Thanks



Public Function GenerateSerialNumber(FullName As String, UniqueID As String) As String
retry:
Randomize
randkey = Int(Rnd * 99 + 1)
If Len(randkey) < 2 Then
    GoTo retry:
End If
serial = randkey & "-"
For X1 = 1 To Len(FullName) Step 3
    j = Mid(FullName, X1, 1)
    p = p & Int(Asc(j) + randkey - 9 / 2 ^ Int(Mid(randkey, 1, 1)))
    If Len(p) > 6 Then
        Exit For
    End If
Next X1
For X2 = 1 To Len(UniqueID) Step 2
    jx = Mid(UniqueID, X2, 1)
    If IsNumeric(jx) = True Then
        jf = jf & Int(jx * randkey / 2 ^ Int(Mid(randkey, 1, 1)) / 3 * randkey)
    Else
        jf = jf & Int(randkey * Mid(Asc(jx), 1, 1) / 2 ^ Int(Mid(randkey, 1, 1)))
    End If
Next X2
serial = serial & p & "-" & jf
GenerateSerialNumber = serial
End Function
Avatar of maneshr
maneshr

BrianGEFF719,

"., i need the following visual basic function converted to perl. .."

Can you explain, in plain english, what exactly you are looking for?

This will dramatically increase your choice of getting a working solution, faster.

Let me know.
Avatar of BrianGEFF719

ASKER

I'm going to move this to the VB Section.
He wants to generate serial number based upon

Full name and Uniqueid which would be passed to the function.

Is it like you want only this logic to be implemented ???

JD
dkjariwala,
That is exactly what i need the perl function to do. All I need is that same function but written in perl. Like he said, I need a perl function to generate serial numbers based upon (name and unique computer id).

If anyone has any questions on this, just let me know.
I have just tried,
I don't know if this is perfect or not.

$number= &generate_serial_number('DivyeshJariwala',23423423423);
print $number;



sub generate_serial_number
{
     my($fullname,$uniqueid) = @_;

     my $randkey = int(rand(99)) + 10; #adding 10 would make sure that randkey is minimum 2 digits.

     my $serial = $randkey . "-";

     my $p_value;

     @array = split(//, $fullname);
     for($i=0;$i<length($fullname);$i+=3)
     {
          $j = substr($fullname,$i,1);
          $power = substr($randkey,1,1);

          $temp = int ( ord($j)  + $randkey - 9 / (  2 ^ $power ) );
          $p .= $temp;
          if(  length($p) > 6 )
          {
               $p_value = $p;
          }
     }
     
     for($i=0;$i<length($uniqueid);$i+=2)
     {
          $jx = substr($uniqueid,$i,1);
          if(!($jx =~ /\D/ ))
          {
               $power = int substr($randkey,1,1);
               $jf .=  int (  ( $jx * $randkey / 2 ^ $power ) / 3 * $randkey );
          }
          else
          {
               $power = int substr($randkey,1,1);
               $jf .= int ( ( $randkey * substr(ord($jx),1,1) ) / 2 ^ $power );

          }
     }
         

     $serial .= $p . '-' . $jf;
     return $serial;
}


JD
i'm going to test this, give me a few min...i'm going to compare outputs with the vb program, and if they are the same output, i will give you points if not ill try to see where the differences are, and tell you because i've got no clue on perl programming


THANKS A LOT FOR YOUR HELP SO FAR!!!!
One thing I would like to tell you that it would give you random serial number everytime cause you are utilizing rand function.

So I don't think you can compare them straight off.

JD
i'm going to test this, give me a few min...i'm going to compare outputs with the vb program, and if they are the same output, i will give you points if not ill try to see where the differences are, and tell you because i've got no clue on perl programming


THANKS A LOT FOR YOUR HELP SO FAR!!!!
yah i can compare, i just generate the serial again but specify the randkey...it will generate the same, but right now i'm getting different serials when i generate...lemme generate some serials for you using the vb function, cuz i've played with it and i can figure out why its generating different...

name: Brian Gaffie
unique id: 51345561

here is a list of some serials that the vb function generated...to test your self just set the randkey. But i dont know perl if i did i would test it my self. The first two digits are the randkey. Thanks!

95-160191165-29172935
43-108139113-192115192231
56-121152126-16398163196
65-130161135-11066110132
61-126157131-965896116
62-127158132-10060100120
31-95126100-200120200240
11-7210377-10060100121
76-141172146-75457590
30-9412599-187112187225
42-107138112-183110183220
38-102133107-300180300361
No they are not same. :(

Tell me about,

p = p & Int(Asc(j) + randkey - 9 / 2 ^ Int(Mid(randkey, 1, 1)))
   

What is the order of precedance here ??


Similarly for

If IsNumeric(jx) = True Then
       jf = jf & Int(jx * randkey / 2 ^ Int(Mid(randkey, 1, 1)) / 3 * randkey)
   Else
       jf = jf & Int(randkey * Mid(Asc(jx), 1, 1) / 2 ^ Int(Mid(randkey, 1, 1)))
   End If

Both the JF statements,
JD

acctually i must say now that you have brought the second part to my attention the part that is IsNumeric, the Unique ID will alawys be numeric so you can remove the else statement...I dont know why i did it taht way when i wrote it.

But I honestly dont know...because i tried converting this to JavaScript and i had some problems...So i dont know why it does this..
Here is modified code,
I am not sure about your statements for value of P and JF.

Which should be divided by what ?
JD
i'm going to try to break it down in the vb function instead of one big math line i'm going to break it down to a bunch of small ones...
Check this,

I guess you should now find it more comfortable to follow.

$number= &generate_serial_number('Brian Gaffie',51345561);
print $number . "\n";
print '95-160191165-29172935';

sub generate_serial_number
{
     my($fullname,$uniqueid) = @_;

     my $randkey = int(rand(99)) + 10; #adding 10 would make sure that randkey is minimum 2 digits.


     #################### for testing
     $randkey = 95;
     ################## you override whatever is generated !!

     my $serial = $randkey . "-";

     my $p_value;

     for($i=1;$i<=length($fullname);$i+=3)
     {
          #j = Mid(FullName, X1, 1)
          $j = substr($fullname,$i,1);

             
      #   p = p & Int(Asc(j) + randkey - 9 / 2 ^ Int(Mid(randkey, 1, 1)))

          $power = int substr($randkey,1,1);
          $p .= int ( ord($j)  + $randkey -  9 /  2 ^ $power   );

          if(length($p) > 6 )
          {
               $p_value = $p;
          }
     }
     

     for($i=1;$i<=length($uniqueid);$i+=2)
     {
     #   jx = Mid(UniqueID, X2, 1)
          $jx = substr($uniqueid,$i,1);

          #jf = jf & Int(jx * randkey / 2 ^ Int(Mid(randkey, 1, 1)) / 3 * randkey)
          $power = int(substr($randkey,1,1));
          $jf .=  int (   $jx * $randkey / 2 ^ $power  / 3 * $randkey );
     }
         

     $serial .= $p_value . '-' . $jf;
     return $serial;
}

JD
hi, I was able to break it down like this, may this will help you.

    xx1 = Asc(j) 'get the ascii value of j
    xx2 = randkey 'set xx2 to randkey
    xx3 = 9 'subtract 9
    xx4 = 2 'divide by 2
    xx5 = Int(Mid(randkey, 1, 1)) 'get the first NUMBER in randkey
    mthfunc = xx1 + xx2 - xx3 / xx4 ^ xx5 'do the math
    mth = Int(mthfunc) 'int
    p = p & mth 'APPEND NUMBER TO P, NOT ADD NUMBER
I dont know how perl works, but in my visual basic program...P IS A STRING not a INTEGER, here is an idea...may perl is handling P as an INTEGER and trying to add it instead of appending the new number to P.

-Brian
I think i got it..in Visual Basic strings work like this..

12345
FFFFF

in perl

01234
FFFFF

in perl strings start at 0...not 1. So that could be the problem, if the randkey is 95, it should be doing ^ 9 instead of ^ 5...tell me if that fixes it?
Still it doesnt work. :(

$number= &generate_serial_number('Brian Gaffie',51345561);
print $number . "\n";
print '95-160191165-29172935';

sub generate_serial_number
{
     my($fullname,$uniqueid) = @_;

     my $randkey = int(rand(99)) + 10; #adding 10 would make sure that randkey is minimum 2 digits.


     #################### for testing
     $randkey = 95;
     ################## you override whatever is generated !!

     my $serial = $randkey . "-";

     my $p_value;

     for($i=1;$i<=length($fullname);$i+=3)
     {
          #j = Mid(FullName, X1, 1)
          $j = substr($fullname,$i,1);
      #   p = p & Int(Asc(j) + randkey - 9 / 2 ^ Int(Mid(randkey, 1, 1)))


        $xx1 = ord(j);                    #         xx1 = Asc(j) 'get the ascii value of j

        $xx2 = $randkey;               ####     xx2 = randkey 'set xx2 to randkey
        $xx3 = 9;                              ####        xx3 = 9 'subtract 9
        $xx4 = 2;                              ####        xx4 = 2 'divide by 2
        $xx5 = int(substr($randkey,1,1)) ; ####        xx5 = Int(Mid(randkey, 1, 1)) 'get the first NUMBER in randkey
        $mthfunc = $xx1 + $xx2 - $xx3 / $xx4 ^ $xx5; ##        mthfunc = xx1 + xx2 - xx3 / xx4 ^ xx5 'do the math

        ####### I want to know what is the order ?? shall i power xx4 by xx5 and divide it by xx3 and then substract it from $xx1 + $xx2 ?
        #### How it shall be done ???
       $mth  = int($mthfunc);    ###mth = Int(mthfunc) 'int
         
          $p .= $mth; ###   p = p & mth 'APPEND NUMBER TO P, NOT ADD NUMBER
          if(length($p) > 6)
          {
               $p_value =$p;
          }


     }
     

     for($i=1;$i<=length($uniqueid);$i+=2)
     {
     #   jx = Mid(UniqueID, X2, 1)
          $jx = substr($uniqueid,$i,1);

          #jf = jf & Int(jx * randkey / 2 ^ Int(Mid(randkey, 1, 1)) / 3 * randkey)
          $power = int(substr($randkey,1,1));
          $jf .=  int (   $jx * $randkey / 2 ^ $power  / 3 * $randkey );
     }
         

     $serial .= $p_value . '-' . $jf;
     return $serial;
}

And I do not understand what you mean to say about those string handling in Perl and VB.

JD
k let me explain..

mid(string,start,length) <-- VB
substr(string,start,length) <-- PERL

if you wanted to get the first charecter in name the start in vb would be 1, but the start in perl is 0....isnt that correct?

lemme show you:

name = "Brian"
msgbox mid(name,1,1) <--- WILL MESSAGE BOX "B"

but in perl, substr($name,1,1) will do "r"...because it starts at 0

thats what i mean..
this also works...

xx1 = Asc(j) + randkey
xx5 = Int(Mid(randkey, 1, 1))
mthfunc = xx1 - 9 / 2 ^ xx5
p = p & Int(mthfunc)
   
Oh, yes,You are very much right.

I did the change, Also I found one bug, corrected it up. but still not working.

$number= &generate_serial_number('Brian Gaffie',51345561);
print $number . "\n";
print '95-160191165-29172935';

sub generate_serial_number
{
     my($fullname,$uniqueid) = @_;

     my $randkey = int(rand(99)) + 10; #adding 10 would make sure that randkey is minimum 2 digits.


     #################### for testing
     $randkey = 95;
     ################## you override whatever is generated !!

     my $serial = $randkey . "-";

     my $p_value;

     $found = 0;
     for($i=0;$i<length($fullname);$i+=3)
     {
          #j = Mid(FullName, X1, 1)
          $j = substr($fullname,$i,1);

        $xx1 = ord(j);                    #         xx1 = Asc(j) 'get the ascii value of j

        $xx2 = $randkey;               ####     xx2 = randkey 'set xx2 to randkey
        $xx3 = 9;                              ####        xx3 = 9 'subtract 9
        $xx4 = 2;                              ####        xx4 = 2 'divide by 2
        $xx5 = int(substr($randkey,0,1)) ; ####        xx5 = Int(Mid(randkey, 1, 1)) 'get the first NUMBER in randkey
        $mthfunc = $xx1 + $xx2 - $xx3 / $xx4 ^ $xx5; ##        mthfunc = xx1 + xx2 - xx3 / xx4 ^ xx5 'do the math

        ####### I want to know what is the order ?? shall i power xx4 by xx5 and divide it by xx3 and then substract it from $xx1 + $xx2 ?
        #### How it shall be done ???
       $mth  = int($mthfunc);    ###mth = Int(mthfunc) 'int
         
          $p .= $mth; ###   p = p & mth 'APPEND NUMBER TO P, NOT ADD NUMBER
          if(length($p) > 6 and ! $found)
          {
               $p_value =$p;
               $found = 1;
          }


     }
     

     for($i=1;$i<=length($uniqueid);$i+=2)
     {
     #   jx = Mid(UniqueID, X2, 1)
          $jx = substr($uniqueid,$i,1);

          #jf = jf & Int(jx * randkey / 2 ^ Int(Mid(randkey, 1, 1)) / 3 * randkey)
          $power = int(substr($randkey,1,1));
          $jf .=  int (   $jx * $randkey / 2 ^ $power  / 3 * $randkey );
     }
         

     $serial .= $p_value . '-' . $jf;
     return $serial;
}


JD

Can you answer that ordering of xx1,xx2 and stuff , see my comment in code.
Honestly, I cannot answer the ordering of xx1 and xx2. When i wrote that i just did a bunch of random math things...I've got no clue how VB is processing it.
Hm...
Actually I am sure that this is where the problem is.

Can you just try to put up braces in VB program and see how it changes the output.

I mean try

p = p & Int(Asc(j) + ( randkey - 9 )/ 2 ^ Int(Mid(randkey, 1, 1)))

or

p = p & Int(Asc(j) + randkey -( 9 / (2 ^ Int(Mid(randkey, 1, 1)))))

something like that and tell me how you have got it finally.

JD
     

hey!!!

p = p & Int(Asc(j) + randkey - (9 / (2 ^ Int(Mid(randkey, 1, 1)))))

that works :)
Okie.
But things still doesnt work.

$number= &generate_serial_number('Brian Gaffie',51345561);
print $number . "\n";
print '95-160191165-29172935';

sub generate_serial_number
{
     my($fullname,$uniqueid) = @_;

     my $randkey = int(rand(99)) + 10; #adding 10 would make sure that randkey is minimum 2 digits.


     #################### for testing
     $randkey = 95;
     ################## you override whatever is generated !!

     my $serial = $randkey . "-";

     my $p_value;

     $found = 0;
     for($i=0;$i<length($fullname);$i+=3)
     {
          #j = Mid(FullName, X1, 1)
          $j = substr($fullname,$i,1);

        $xx1 = ord(j);                    #         xx1 = Asc(j) 'get the ascii value of j
        $xx2 = $randkey;               ####     xx2 = randkey 'set xx2 to randkey
        $xx3 = 9;                              ####        xx3 = 9 'subtract 9
        $xx4 = 2;                              ####        xx4 = 2 'divide by 2
        $xx5 = int(substr($randkey,0,1)) ; ####        xx5 = Int(Mid(randkey, 1, 1)) 'get the first NUMBER in randkey
        $mthfunc = $xx1 + $xx2 - ( $xx3 / ($xx4 ** $xx5) ); ##        mthfunc = xx1 + xx2 - xx3 / xx4 ^ xx5 'do the math

         
         
        ####### I want to know what is the order ?? shall i power xx4 by xx5 and divide it by xx3 and then substract it from $xx1 + $xx2 ?
        #### How it shall be done ???
       $mth  = int($mthfunc);    ###mth = Int(mthfunc) 'int
         
          $p .= $mth; ###   p = p & mth 'APPEND NUMBER TO P, NOT ADD NUMBER
          if(length($p) > 6 and ! $found)
          {
               $p_value =$p;
               $found = 1;
          }


     }
     

     for($i=1;$i<=length($uniqueid);$i+=2)
     {
     #   jx = Mid(UniqueID, X2, 1)
          $jx = substr($uniqueid,$i,1);

          #jf = jf & Int(jx * randkey / 2 ** Int(Mid(randkey, 1, 1)) / 3 * randkey)
          $power = int(substr($randkey,1,1));
          $jf .=  int (   $jx * $randkey / 2 **$power  / 3 * $randkey );
     }
         

     $serial .= $p_value . '-' . $jf;
     return $serial;
}

This code doesnt work.
I guess ^ is used to raise the power right ?
In perl ^ is used for binary xor !!
I have corrected it, and actual operator is ** .

But still doesnt work.
Tell me value of xx1,xx2,xx3,xx4,xx5.

No matter what this needs to be solved.

JD
im going to try to get some output, step by step, so we can compare and find out exactly what line the differences are occouring...
That would be better. :)

JD
Okay i've got some output for you...here is what i used to generate the output...and the output its self...This code generated the serial properly...so here you go.


--VB CODE

randkey = 95
serial = randkey & "-"
Print #1, "Randkey: " & randkey
Print #1, "Starting FOR LOOP"
For X1 = 1 To Len(FullName) Step 3
    j = Mid(FullName, X1, 1)
    Print #1, "Current Letter: " & j
    xx1 = Asc(j) + randkey
    Print #1, "xx1=" & xx1
    xx2 = 2 ^ Int(Mid(randkey, 1, 1))
    Print #1, "xx2=" & xx2
    xx3 = xx1 - 9 / xx2
    Print #1, "xx3=" & xx3
    p = p & Int(xx3)
    Print #1, "p=" & p
    If Len(p) > 6 Then
        Exit For
    End If
Next X1
Close #1

--OUTPUT.TXT OUTPUT:

Randkey: 95
Starting FOR LOOP
Current Letter: B
xx1=161
xx2=512
xx3=160.982421875
p=160
Current Letter: a
xx1=192
xx2=512
xx3=191.982421875
p=160191
Current Letter: G
xx1=166
xx2=512
xx3=165.982421875
p=160191165


Hey, I just had someone convert it to C++, if this helps you at all....I'm really trying to get this done in Perl...

-Brian


char * generateserialnumber(char *fullname, char *uniqueid, char * serial)
{
    long randkey, x1,x2,lval;
    char p[4096];
    char sval[4096];
    char stemp[80];
    char jf[80];
    char j, jx;

    double dval;

    *p = 0;
    *sval = 0;
    *stemp = 0;
    *jf = 0;
    srand( (unsigned) time(NULL) );
     
     do{
      randkey=rand() % 100;
    } while (randkey < 10);

    sprintf(serial, "%li-", randkey);
   
     for(x1=1; x1 <= (signed long) strlen(fullname); x1+=3)
    {
         j = fullname[x1 - 1];
         dval =  9 / pow(2,int(randkey/10));
         lval = int((double) j + randkey - dval);
         sprintf(sval,"%li",lval);
         strcat(p,sval);
         if (strlen(p) > 6)
              break;

    }

    for(x2=1; x2 <= (signed long) strlen(uniqueid); x2+=2)
    {
         jx = uniqueid[x2 - 1];
         if (jx >= '0' && jx <= '9')
         {
              dval = pow(2,randkey / 10);
              lval = int((double) (jx - '0') * randkey / dval /3 * randkey);
           }
         else
         {          
              sprintf(stemp,"%i", (int) jx);
              dval = pow(2,randkey / 10);
              lval = int( (double) randkey * (stemp[0] - '0') / dval);                    
           }        
         sprintf(sval,"%li",lval);
         strcat(jf,sval);
    }
    strcat(serial, p);
    strcat(serial, "-");
    strcat(serial, jf);
    return serial;
}
Hey, I just had someone convert it to C++, if this helps you at all....I'm really trying to get this done in Perl...

-Brian


char * generateserialnumber(char *fullname, char *uniqueid, char * serial)
{
    long randkey, x1,x2,lval;
    char p[4096];
    char sval[4096];
    char stemp[80];
    char jf[80];
    char j, jx;

    double dval;

    *p = 0;
    *sval = 0;
    *stemp = 0;
    *jf = 0;
    srand( (unsigned) time(NULL) );
     
     do{
      randkey=rand() % 100;
    } while (randkey < 10);

    sprintf(serial, "%li-", randkey);
   
     for(x1=1; x1 <= (signed long) strlen(fullname); x1+=3)
    {
         j = fullname[x1 - 1];
         dval =  9 / pow(2,int(randkey/10));
         lval = int((double) j + randkey - dval);
         sprintf(sval,"%li",lval);
         strcat(p,sval);
         if (strlen(p) > 6)
              break;

    }

    for(x2=1; x2 <= (signed long) strlen(uniqueid); x2+=2)
    {
         jx = uniqueid[x2 - 1];
         if (jx >= '0' && jx <= '9')
         {
              dval = pow(2,randkey / 10);
              lval = int((double) (jx - '0') * randkey / dval /3 * randkey);
           }
         else
         {          
              sprintf(stemp,"%i", (int) jx);
              dval = pow(2,randkey / 10);
              lval = int( (double) randkey * (stemp[0] - '0') / dval);                    
           }        
         sprintf(sval,"%li",lval);
         strcat(jf,sval);
    }
    strcat(serial, p);
    strcat(serial, "-");
    strcat(serial, jf);
    return serial;
}
sorry couldnt reply.
i was bit too occupied @ office.
i shall try something @ home and let you know,
JD
ASKER CERTIFIED SOLUTION
Avatar of dkjariwala
dkjariwala

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20249647.html
https://www.experts-exchange.com/questions/Q.20252182.html
https://www.experts-exchange.com/questions/Q.20276179.html
https://www.experts-exchange.com/questions/Q.20281492.html
https://www.experts-exchange.com/questions/Q.20281343.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
Your response to these requests is very much appreciated.
:) Moondancer - EE Moderator