flee@dictionopolis.cs.psu.edu (Felix Lee) (10/29/90)
>I'd much rather add a pair() operator. Or some generalization thereof. > print join("\n", pair('=', %ENV)); How about apply(N, EXPR, LIST), apply EXPR to every N items of LIST and return a list of the results. EXPR can refer to the N items using the @_ array. Below, an incredibly life-like simulation. sub apply { local($n, $expr, @v) = @_; local(@_, @u); eval ' while (@v) { @_ = splice(@v, $[, $n); push(@u, ('.$expr.')); } 1' || warn "$@"; @u; } @ENV = &apply(2, '@_[0]."=".@_[1]', %ENV); print join("\n", @ENV), "\n"; @a = ( 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9, ); @v = &apply(4, '@_[0]+@_[1]+@_[2]+@_[3]', @a); print "(@v)\n"; @pp = (); push(@pp, @p) while @p = getpwent; @n = &apply(9, '&gcos', @pp); sub gcos { local($_) = @_[6]; s/,.*//; s/&/substr(@_[0], 0, 1) =~ tr:a-z:A-Z:, @_[0]/eg; $_; } print join("\n", @n), "\n"; Note, I use @_[0] instead of $_[0] because it's more natural for me to read (@_)[0] rather than ${_[0]}, given that ${_} is something entirely different. I wish I could say %foo{'bar'} too. The EXPRs above are pretty nasty. Isn't it awkward to say things like "@_[0]" all the time? Wouldn't it be nice if you could say "@0" instead? Let's hear it for the unary array dereferencing operator: @ EXPR ==> @_[EXPR] or @ARGV[EXPR] Easily generalizable to the binary array dereferencing operator: WORD @ EXPR ==> @WORD[EXPR] EXPR @ EXPR ==> (EXPR) [EXPR] More clutter for the perl grammar. Whee. Forget all the above. I'll settle for just pair(). -- Felix Lee flee@cs.psu.edu