in reply to Is Perl less compact than Ruby? (Kind of competition)

By folding the suggestions encountered so far, and adding in some of my own, I was able to bring the total down to 342 characters. Big gains can be made by observing that $foo[-2] refers to indices at the end of an array. That happens twice in this script.

In the part where the code pops to $_ and $0, it's cheaper to just call pop @n directly. This was the case with pop@n, and I guess it will still hold with p.

sub w as written contains a bug. Sometimes you push more than one element on. But that's okay, @_ is a win over $_[0].

I tried changing expr if cond to cond&&expr but that didn't do anything.

map{push@L,@C+0;push@C,lc=~/./g}<>;sub p{pop@n}sub w{push@n,@_}while($ +n<@C){$_= $C[$n++];if(/n/){$0=0;/-/ or$0=$_+$0*7while($_=index"htaoinse",$C[$n++ +])<7;w$0} if(/t/){$_=p;$_?$n=$L[$_-1]:last if p}/a/&&w 1+grep{$n>$_}@L;if(/h/){$ +_=p;w$n[- abs$_];$_>0&&splice@n,-1-$_,1}/o/&&print chr p;/s/&&w-p()+p;/e/&&w int + p()/p,p% p;w($_=getc)?ord:-1if/i/}

Note that changing /s/&&v-(p)+p to /s/&&v+p-p breaks the interpreter. Or at least, the hello.eta program no longer runs correctly, which was the extent of my testing. The interpreter is quite slow.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Replies are listed 'Best First'.
negative offsets if good
by locked_user mtve (Deacon) on Feb 19, 2002 at 07:42 UTC

    but a few bugs exists.

    p has side effect of modifying @n, so for example in /e/ part you must call it only twice not four times.

    in last expression correct logic is w+($_=getc)?ord:-1if/i/