I'm translating some python(2) code into perl, and I'm hoping there are some bilingual sorts who can help. (It's an interesting poetry writing program that I don't think I can begin to understand until I at least have it in a language I understand. Plus I'm hoping it'll run faster in Perl. Otherwise I wouldn't be undertaking this labor of love. :-p )
One of the biggest headaches is translating Python's list range expressions into equivalent Perl array expressions. In Python you can write:
In Perl, using List::Util qw/head tail/, the equivalent is:a = [ 'a','b','c','d','e','f','g' ] a[:3] # abc a[:-3] # abcd a[3:] # defg a[-3:] # efg
Of course, most of the time the code contains a variable, not a hard-coded 3. And then my solution breaks on head(-$p, @a) and tail(-$p, @a) when $p is 0.my @a = qw/a b c d e f g/; # python a[:3] = 'abc' (ie. select first 3) say join( '', head( 3, @a)) . " should be abc"; # python a[:-3] = 'abcd' (ie. omit last 3) say join( '', head( -3, @a)) . " should be abcd"; # except when -0! # python a[3:] = 'defg' (ie. omit first 3) say join( '', tail( -3, @a)) . " should be defg"; # except when -0! # python a[-3:] = 'efg' (ie. select last 3) say join( '', tail( 3, @a)) . " should be efg";
Is there a CPAN module that might support python-like specification of array ranges in a concise manner?
In reply to Converting python list range expressions to perl by ibm1620
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |