[comp.lang.perl] a2p generates incorrect code

dcd@tc.fluke.COM (David Dyck) (08/02/90)

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

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (08/03/90)

In article <1990Aug2.090725.2029@tc.fluke.COM> dcd@tc.fluke.COM (David Dyck) writes:
: 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)"
: 
: I don't mind perl not simulating the random order that
: awk uses, but is there a better way to do this?

After the next patch it will spit out
	foreach $i ($[ .. $#list)

: 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

Hmm, not too bad for un-idiomatic Perl.  I bet it could be made to
run twice as fast with a little perlish polish.

Larry