in reply to (jeffa) Re: Converting Python to Perl and back again
in thread Converting Python to Perl and back again

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.

Replies are listed 'Best First'.
(jeffa) 3Re: Converting Python to Perl and back again
by jeffa (Bishop) on Apr 04, 2003 at 13:32 UTC
    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)