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: -
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.org)
8 1745 KevinADC 4,059
Recognized Expert Specialist
Use your favorite text editor. ;)
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.
Sorry for double posting but I think I figured it out....at least somewhat. -
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...
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.
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?
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: - 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 ;)
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.
I assume the Perl script is calculating the total for the ascii of each letters in the key?
Here's a Python eg. -
#!/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
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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....
|
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...
|
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
|
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...
| |
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...
|
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:
...
|
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;
|
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?
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |