in reply to Converting Python to Perl and back again

Well, what particular problems? What particular piece of Python code? I can tell you one thing for sure, any code that converts Python to Perl is broken at best. There probably will not be any such tool that is worthy until a Python binding for Parrot is completed.

I can say that a Python tuple is analogous to a Perl list, not a Perl array (a Python list is what we Perl folk call an array). As for converting complex data structures ... well ... Perl is not really that much different from Python, at least it doesn't have to be:

Python:

thingy = [ { 'artist' : 'Genesis', 'genre' : ['pop','rock','progressive'], 'albums' : [ { 'title' : 'Foxtrot', 'year' : 1972 }, { 'title' : 'Duke', 'year' : 1980 }, { 'title' : 'Genesis', 'year' : 1983 }, ], }, ]

Perl:

$thingy = [ { artist => 'Genesis', genre => [qw(pop rock progressive)], albums => [ { title => 'Foxtrot', year => 1972 }, { title => 'Duke', year => 1980 }, { title => 'Genesis', year => 1983 }, ], }, ];
We can help you convert Python code to Perl, but i doubt you'll get much help from us converting Perl code to Python. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Converting Python to Perl and back again
by Anonymous Monk on Apr 04, 2003 at 06:43 UTC

    Hi, thanks for the reply. Sorry I didn't provide examples of the code but the powers that be won't let me you see (they try to shut me down on MTV.. heh, sorry). Your example helped a lot though, and I have most of it done. Just two more questions with regards to the data structures, could you let me know how close these are:

    name = thingy['albums'][0]['title'] $name = $thingy{albums}[0]{title}

    Should both return 'Foxtrot' (without the quotes), correct?

    And as for populating the structures, could I do something such as the following:

    thingy['artistName'][0]['year'] = 1987 or thingy{'artistName}[0]{'year'} = 1987

    And if I did it this way, how would I go about populating multiple entries in the album field, would I have to use a counter, or is there some other way? Thanks again.

      Your first snippet is close, but thingy is a an array/list, not a hash/dictionary, so you need to index the very first item in that array/list, which is a hash/dictionary:
      name = thingy[0]['albums'][0]['title'] my $name = $thingy->[0]{albums}[0]{title};
      For your second snippet, you will need to loop through each hash/dictionary that is contained inside the 'album' array/list:
      for a in thingy[0]['albums'] : a['year'] = 1999 $_->{year} = 1999 for @{$thingy->[0]->{albums}};
      As you can see, you don't need a counter, you only need to process each element at a time. Perl and Python are both good about giving the coder 'ease of iteration'. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: (jeffa) Re: Converting Python to Perl and back again
by Anonymous Monk on Apr 04, 2003 at 08:01 UTC

    Okay, I've got another fine Python question for you wise Perlmonks (well, know thy enemy, right? ;-). Say I want data such as:

    genesis = { "Foxtrot":({"instrumental":"Okay"}, {"vocals":"Good"}), "D +uke":({"vocals":"Better"}) }

    How would I go about initially populating the dictionary? Say, as if I was reading from a database and looping over it. There isn't an insert or append operator for dictionaries, and I can't seem to just go: genesis["Foxtrot"]["instrumental"] = "Okay" Any ideas? Thanks.

      That's right, what you have is illegal syntax in either language. You are trying to access a HoH (hash of hashes) instead of what you really have, a LoH (list of hashes). You simply are missing the array index:
      genesis['Foxtrot'][0]['instrumental'] = 'excellent' $gensis->{Foxtrot}[0]{instrumental} = 'excellent';
      Now then, as for populating these datastructures from a database ... well, i can't help you much with Python. You'll have to read the docs, but i can say that Perl's DBI has methods that will form these datastructures for you, methods such as:
      • selectall_arrayref
      • selectall_hashref
      • fetchrow_arrayref
      • fetchrow_hashref
      and more. I usually only use selectall_arrayref, but sometimes i need the lookup power of hashes. You have to ensure that all of your data has unique id's in order for hashes to work safely (without clobbering existing keys). Recently i decided to roll my own:
      # takes prepared statement handle and returns LoH sub db2tmpl { my ($sth) = @_; $sth->execute; my $fields = $sth->{NAME}; my $rows = $sth->fetchall_arrayref; my @loh; for my $row (@$rows) { my %hash; @hash{@$fields} = @$row; push @loh, {%hash}; } return \@loh; }
      This is very useful with HTML::Template, though i can't help but feel i am re-inventing a wheel. At any rate, i wish you the best of luck. :)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        Thanks for reply. Sorry for the confusion, what I meant by populating the datastructure was the actual looping over and adding entries (this seems pretty straightforward now given your other post). I don't have any problems with reading the data from the database, the docs were decent in both languages for that :).

Re: (jeffa) Re: Converting Python to Perl and back again
by Anonymous Monk on Apr 04, 2003 at 19:52 UTC

    Thanks for all the help, I think I've got it now :)