Perl to Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mage1989
    New Member
    • Mar 2007
    • 11

    Perl to Python

    Hey, I have a question. I have been learning python recently, and am wondering how I would convert some Perl code I have to python. There may not even be a way, I am just looking for any information.

    Here is the perl:

    Code:
    sub validkey {
    	if (not($_[0] =~ /[A-Z]|[a-z]|[0-9]/)) {
    		return 0;
    	}
    	my @idchars = split(//, $_[0]);
    	my ($total, $counter, $char) = (0, 0);
    	while (defined($idchars[$counter])) {
    		$char = $idchars[$counter];
    		$total += (ascii($char)+($total*$counter));
    		$counter++;
    	}
    
    	if ($total > 925559 && $total < 927901) {
    		return $total;
    	} else {
    		return 0;
    	}
    }
    
    sub ascii {
    	my (@str, $pos, $offset);
    	if ($_[0] =~ /[0-9]/) {
    		@str = split(//, '0123456789');
    		$offset = 48;
    
    	} elsif ($_[0] =~ /[A-Z]/) {
    		@str = split(//, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
    		$offset = 65;
    
    	} elsif ($_[0] =~ /[a-z]/) {
    		@str = split(//, 'abcdefghijklmnopqrstuvwxyz');
    		$offset = 97;
    
    	} else {
    		return 0;
    	}
    
    	$pos = 0;
    	while (defined($str[$pos])) {
    		if ($_[0] eq $str[$pos]) {
    			return ($pos+$offset);
    		}
    		$pos++;
    	}
    }
    (BTW This is NOT my code. It belongs to hackthissite.or g)
    Last edited by miller; Mar 5 '07, 05:52 PM. Reason: Code Tag and Formatting
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Use your favorite text editor. ;)

    Comment

    • Mage1989
      New Member
      • Mar 2007
      • 11

      #3
      Originally posted by KevinADC
      Use your favorite text editor. ;)
      My favorite text editor?

      Meaning? just change it manually? or something else.

      Sorry bro. At the moment I have the brain capacity of a cat. Tired lol.

      Comment

      • Mage1989
        New Member
        • Mar 2007
        • 11

        #4
        Sorry for double posting but I think I figured it out....at least somewhat.

        Code:
        from Tkinter import *
        from os import *
        from string import *
        
        letter = "abc"
        
        def ascii():
            my (string, pos, offset);
            if (letter[0] == [0-9]):
                string = split( '0123456789');
                offset = 48;
            elif (letter[0] == [A-Z]):
                string = split('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
                offset = 65;
            elif (letter[0] == [a-z]):
                string = split( 'abcdefghijklmnopqrstuvwxyz');
                offset = 97;
            else:
                return 0;
                pos = 0;
            while (defined(string[pos])):
                if (letter[0] == string[pos]):
                    return (pos+offset);
                    pos + 1
        
                    
        if (not(letter[0] == [A-Z]|[a-z]|[0-9])):
            idchar = split(letter[0]);
            (total, counter, char) = (0, 0);
            while(defined(idchar[counter])):
                char = idchar[counter];
                total += (ascii(char)+(total*counter));
                counter + 1
        
        if (total > 925559 and total < 927901):
            print total;
        else:
            print "bob"
        Honestly, I don't think this will do anything. I am getting an error when I run it. It says that A is not defined on line 27...

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          I was being silly in my first post. The way to change any script is to use a text editor. Maybe someone here knows enough python to help you with you code, I don't know pyton at all.

          Comment

          • Mage1989
            New Member
            • Mar 2007
            • 11

            #6
            Originally posted by KevinADC
            I was being silly in my first post. The way to change any script is to use a text editor. Maybe someone here knows enough python to help you with you code, I don't know pyton at all.

            Ok, Thank you for your help :)

            I decided to just learn pearl and try it in pearl.

            I have a question about something I can't find in my tutorial.

            if (not($_[0] =~ /[A-Z]|[a-z]|[0-9]/)) in this line of code. What does the 'not' do?
            is it saying

            If the first element in @$_ is not in the =~ /[A-Z]|[a-z]|[0-9] list, then proceed?

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              'not' is an operator, same as '!' but lower precedence.

              that says: "if $_[0] does not match whats on the right side of '=~' anywhere in the string" it's true and to do whatever block or expression follows.

              better written in perl as:

              Code:
              if ( $_[0] !~ /\w/) {
                 do something useful
              }
              it's a test to make sure $_[0] has at least one character in the a-zA-Z0-9 range (same as \w which is a short cut character class for [a-zA-Z0-9]). But it does not test where in the string.

              A link to help you with perl: http://perldoc.perl.org/index-tutorials.html

              bookmark that site if you decide to continue with perl ;)

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                small correction...

                \w is the short cut character class for: a-zA-Z0-9_

                the underscore character '_' is included in \w.

                Comment

                • ghostdog74
                  Recognized Expert Contributor
                  • Apr 2006
                  • 511

                  #9
                  I assume the Perl script is calculating the total for the ascii of each letters in the key?
                  Here's a Python eg.
                  Code:
                  #!/usr/bin/python
                  import sys,string
                  printable = string.printable[0:62] # get 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
                  def validkey(key):
                          total = 0
                          for ch in key:
                                  if ch in printable:
                                          total = total + ord(ch) #add ascii values of the key letters
                                  else:
                                          total = 0
                                          break ##come out after detecting an invalid key
                          return total
                  
                  result = validkey("testing")
                  if result == 0:
                          print "Invalid character found"
                  else:
                          print "total is " , result

                  Comment

                  Working...