Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mit-eddie!uw-beaver!fluke!dcd From: dcd@tc.fluke.COM (David Dyck) Newsgroups: comp.lang.perl Subject: a2p generates incorrect code Keywords: a2p awk perl bug for Message-ID: <1990Aug2.090725.2029@tc.fluke.COM> Date: 2 Aug 90 09:07:25 GMT Organization: John Fluke Mfg. Co., Inc., Everett, WA Lines: 51 a2p generates incorrect code while translating for loop. --------- example script "t1"------- BEGIN { split("a b c", list); for (i in list) { print i; } } ---------- end of "t1" ---------------- # when run from awk with "awk -f t1 < /dev/null" it prints # the array indices in 'some' order. 2 3 1 # when translated to perl via "a2p t1 > t1.pl" and executes # with the command "perl t1.pl < /dev/null" the # array elements themselves are printed a b c The awk "for (i in list)" is translated into "foreach $i (@list)" To get it to run more like awk I changed the perl code to read "for ($i=1; $i <= $#list; ++$i)", and then perl prints: 1 2 3 I don't mind perl not simulating the random order that awk uses, but is there a better way to do this? PS. (I came upon this while trying to convert the 'Lisp interpreter in Awk' that was posted to comp.lang.lisp and alt.sources into 'Lisp in perl'. After correcting the bug, perl was only a little slower that awk). AWK: real 0m5.03s user 0m4.73s sys 0m0.11s PERL: real 0m5.10s user 0m4.78s sys 0m0.21s David Dyck Domain: dcd@tc.fluke.COM Voice: +1 206 356 5807 UUCP: {uunet,uw-beaver,decwrl,microsof,sun}!fluke!dcd Snail: John Fluke Mfg. Co. / P.O. Box 9090 / Everett WA 98206-9090 / USA