[Tutor] About Perl's Integer module

Mohamed Lrhazi mohamed at your-site.com
Fri Dec 17 21:04:05 CET 2004


Hello all,

I ported to python a little Perl script that applies some math algorithm 
that I do not understand... My version seems to give the same results as 
the Perl version... but just to make sure I am asking the following:

The Perl version starts by testing whether Perl is in integer mode or not, 
and if not it exists! Is there an equivalent module in Python?

>From what I gathered, in Integer mode, Perl convert (floors) all numbers to 
integers while doing any arithmetic (+ - * / bit shifting...)

Should I simply wrap every number involved in any arithmetic calculation 
with calls to floor()?

My python script, and the Perl original, follow. What it does is create a 
hash of the input string (which is supposed to be a DNS domain name) and 
returns the two layer directory tree where the domain should live in my 
filesystem :

site.company.com -->  X3/32/site.company.com
site2.company.com --> 6U/zv/site2.company.com

Given the lack of "use integer" from my code... can anyone tell these two 
programs are equivalent?


def complex(domain):
	h=0
	res=""
	domain=domain.lower()
	prefix=['x','x','/','x','x','/']
	conv=[
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
		'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
		'Y', 'Z',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
		'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
		'y', 'z',
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'-', '_'
	]

	for i in range(0,len(domain)):
		h*=129
		h+=ord(domain[i])
		h+=987654321
	if h == 0:
		h=1
	prefix[0] = conv[ h & 0x3f ]; h = h >> 6
	prefix[1] = conv[ h & 0x3f ]; h = h >> 6
	prefix[3] = conv[ h & 0x3f ]; h = h >> 6
	prefix[4] = conv[ h & 0x3f ];
	
	return "".join(prefix) + domain
	
print complex(sys.argv[1])

""" The Perl version :
# ----------------------------------------------------------
# Returns the complex hash of the website.

sub complex {

    my $site = shift;
    my $h = 0;
    my $res = "";

    $site = lc $site;

    my @prefix = ( 'x','x','/','x','x','/' );
    my @conv   = (
		  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
		  'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
		  'Y', 'Z',
		  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
		  'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
		  'y', 'z',
		  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		  '-', '_'
		  );

    my @chars = split //, $site;

    my $i;
    for( $i = 0; $i < $#chars + 1; $i++ ) {
	$h *= 129;
	$h += ord( $chars[$i] );
	$h += 987654321;
    }
    if( $h == 0 ) { $h = 1; }

    $prefix[0] = $conv[$h & 0x3f]; $h = $h >> 6;
    $prefix[1] = $conv[$h & 0x3f]; $h = $h >> 6;
    $prefix[3] = $conv[$h & 0x3f]; $h = $h >> 6;
    $prefix[4] = $conv[$h & 0x3f];

    return (join '', @prefix) . "$site";
}
"""



More information about the Tutor mailing list