473,480 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Perl to Python

11 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. sub validkey {
  2.     if (not($_[0] =~ /[A-Z]|[a-z]|[0-9]/)) {
  3.         return 0;
  4.     }
  5.     my @idchars = split(//, $_[0]);
  6.     my ($total, $counter, $char) = (0, 0);
  7.     while (defined($idchars[$counter])) {
  8.         $char = $idchars[$counter];
  9.         $total += (ascii($char)+($total*$counter));
  10.         $counter++;
  11.     }
  12.  
  13.     if ($total > 925559 && $total < 927901) {
  14.         return $total;
  15.     } else {
  16.         return 0;
  17.     }
  18. }
  19.  
  20. sub ascii {
  21.     my (@str, $pos, $offset);
  22.     if ($_[0] =~ /[0-9]/) {
  23.         @str = split(//, '0123456789');
  24.         $offset = 48;
  25.  
  26.     } elsif ($_[0] =~ /[A-Z]/) {
  27.         @str = split(//, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  28.         $offset = 65;
  29.  
  30.     } elsif ($_[0] =~ /[a-z]/) {
  31.         @str = split(//, 'abcdefghijklmnopqrstuvwxyz');
  32.         $offset = 97;
  33.  
  34.     } else {
  35.         return 0;
  36.     }
  37.  
  38.     $pos = 0;
  39.     while (defined($str[$pos])) {
  40.         if ($_[0] eq $str[$pos]) {
  41.             return ($pos+$offset);
  42.         }
  43.         $pos++;
  44.     }
  45. }
  46.  
(BTW This is NOT my code. It belongs to hackthissite.org)
Mar 5 '07 #1
8 1745
KevinADC
4,059 Recognized Expert Specialist
Use your favorite text editor. ;)
Mar 5 '07 #2
Mage1989
11 New Member
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.
Mar 5 '07 #3
Mage1989
11 New Member
Sorry for double posting but I think I figured it out....at least somewhat.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from os import *
  3. from string import *
  4.  
  5. letter = "abc"
  6.  
  7. def ascii():
  8.     my (string, pos, offset);
  9.     if (letter[0] == [0-9]):
  10.         string = split( '0123456789');
  11.         offset = 48;
  12.     elif (letter[0] == [A-Z]):
  13.         string = split('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  14.         offset = 65;
  15.     elif (letter[0] == [a-z]):
  16.         string = split( 'abcdefghijklmnopqrstuvwxyz');
  17.         offset = 97;
  18.     else:
  19.         return 0;
  20.         pos = 0;
  21.     while (defined(string[pos])):
  22.         if (letter[0] == string[pos]):
  23.             return (pos+offset);
  24.             pos + 1
  25.  
  26.  
  27. if (not(letter[0] == [A-Z]|[a-z]|[0-9])):
  28.     idchar = split(letter[0]);
  29.     (total, counter, char) = (0, 0);
  30.     while(defined(idchar[counter])):
  31.         char = idchar[counter];
  32.         total += (ascii(char)+(total*counter));
  33.         counter + 1
  34.  
  35. if (total > 925559 and total < 927901):
  36.     print total;
  37. else:
  38.     print "bob"
  39.  
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...
Mar 5 '07 #4
KevinADC
4,059 Recognized Expert Specialist
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.
Mar 5 '07 #5
Mage1989
11 New Member
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?
Mar 5 '07 #6
KevinADC
4,059 Recognized Expert Specialist
'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:

Expand|Select|Wrap|Line Numbers
  1. if ( $_[0] !~ /\w/) {
  2.    do something useful
  3. }
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 ;)
Mar 5 '07 #7
KevinADC
4,059 Recognized Expert Specialist
small correction...

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

the underscore character '_' is included in \w.
Mar 5 '07 #8
ghostdog74
511 Recognized Expert Contributor
I assume the Perl script is calculating the total for the ascii of each letters in the key?
Here's a Python eg.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import sys,string
  3. printable = string.printable[0:62] # get 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  4. def validkey(key):
  5.         total = 0
  6.         for ch in key:
  7.                 if ch in printable:
  8.                         total = total + ord(ch) #add ascii values of the key letters
  9.                 else:
  10.                         total = 0
  11.                         break ##come out after detecting an invalid key
  12.         return total
  13.  
  14. result = validkey("testing")
  15. if result == 0:
  16.         print "Invalid character found"
  17. else:
  18.         print "total is " , result
  19.  
  20.  
Mar 6 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2111
by: Roy Smith | last post by:
I'm working on a prototype of a new application in Python. At some point, if this ever turns into a product, the powers that be will almost certainly demand that it be done in Perl. My job will...
42
4019
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
46
3236
by: Reinhold Birkenfeld | last post by:
Hello, another Perl/Python question: the subject says it all. Perl is going to change dramatically to become a more powerful and easier to (read|write) language. Is Python taking a similar...
17
3071
by: Michael McGarry | last post by:
Hi, I am just starting to use Python. Does Python have all the regular expression features of Perl? Is Python missing any features available in Perl? Thanks, Michael
31
4719
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
9
4500
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
20
4017
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
13
2065
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
12
2313
by: rurpy | last post by:
Is there an effcient way (more so than cgi) of using Python with Microsoft IIS? Something equivalent to Perl-ISAPI?
8
2414
by: Palindrom | last post by:
Hi everyone ! I'd like to apologize in advance for my bad english, it's not my mother tongue... My girlfriend (who is a newbie in Python, but knows Perl quite well) asked me this morning why...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.