Hi there
I have following hash related code in Perl that i need to translate in Python

if ( exists $legs->{$reference} ) {
        logg(" 2nd leg of swapes: $ref - merging");
        my $swap = merge_swap_legs( $legs->{$ref}, $fields );
        $ref = new_fk_ref( $fxall, $ref, $swap );
      } else {
        $legs->{$ref} = $fields;
        logg("1st leg of swap: $ref");

Can anyone help me how i can translate above code into PYTHON?
Waiting for help from some one. Thanks
Regards

Recommended Answers

All 2 Replies

A Google of "Perl to Python" will yield some of the programs that will do this for you. If the translation is incomplete, post the offending code, with an explanation of what it should do, and someone will help. Note that this code appears to call other functions, so just translating this portion will probably not work.

# Very Loose translation, beware of the globals() and NEVER explicitly translate Perl to Python. See why below.

# Assumes that legs, the var referenced by legs, reference, ref, fields, fxall, merge_swap_legs, new_fk_ref, and logg exist
if legs in globals():
	legs = globals()[legs]
else:
	legs = locals()[legs]

if reference < len(legs): # if reference in legs if legs is a dictionary (if is a hash)
	logg("2nd leg of swapes: %s - merging"%ref)
	swap = merge_swap_legs(legs[ref], fields)
	ref = new_fk_ref(fxall,ref,swap)
else:
	legs[ref] = fields
	logg("1st leg of swap: %s"%ref)

"""
legs is a string
The variable referenced by legs is a list/dictionary
reference is an integer/string (if ^ is list/dictionary)
ref is of same type as reference
fields can be any object
fxall is arbitrary
Everything else are functions
"""

This however is a horrible coding style, post the complete code and I'll see if it can be easily built in python.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.