Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Processing files / Any AWK users?

From: joel:neely:fedex at: 19-Nov-2001 6:47

Hi, Gregg, (popping my head up from the project from the black lagoon for a mo') Gregg Irwin wrote:
> If I'm processing a file, by lines, thus: > > foreach line read/lines/with file rec-sep [ > > It appears that foreach doesn't call read on each pass, but I thought > I'd ask to see if anyone knows, for certain. I know you can modify > the block that foreach uses and it will see the changes, but I wasn't > sure what magic occurs in this scenario. >
There's no interaction at all between FOREACH and READ. The above code is exactly equivalent to tempfoo: read/lines/with file recsep foreach line tempfoo [ (except that this latter case burns up a word name). Another way to think of it is foreach line (read/lines/with file rec-sep) [ to emphasize that READ/LINES just creates a block of strings. Then (on completion) that block is used as the second arg to FOREACH.
> The real question, I suppose, is: Should I cache the return value > from 'read before using it with foreach, or is that unnecessary? >
Totally unnecesary (and actually wastes a few nanoseconds ;-) unless you want to do something else with the saved block after FOREACH gets through doing its thing (in which case you already know that the answer is "Yes").
> As a side question, does anyone on the list use AWK or think that > something like it (i.e. an auto-driven rule/action file processor) > would be useful? >
It would be marginally useful to me as a keystroke-saver, but a brute-force equivalent in REBOL would (IMHO) only result in replacing something vaguely like awk-like-func: func [one-line [string!] ...] [ if parse/all/case one-line [ ;...first parse rule... ][ ;...corresponding action... exit ] if parse/all/case one-line [ ; parse rule... ][ ;...corresponding action... exit ] ;... if parse/all/case one-line [ ;...last parse rule... ][ ;...corresponding action... exit ] ;...error or inaction... ] foreach myline read myfile [awk-like-func myline] with do %awklib.r awk-plan: [ [ ;...local words for rules/actions] [ ;...first parse rule...] [ ;...corresponding action...] [ ; parse rule... ] [ ;...corresponding action...] ;... [ ;...last parse rule... ] [ ;...corresponding action...] ] awk-main myfile awk-plan which would be handy but not a huge win. I guess it just depends on whether most of one's parsing/action tasks are line oriented or not. However, I don't think AWK-PLAN would be too challenging to write. -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;