From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from SINet.SLB.COM ([163.185.18.1]) by hawkwind.utcs.toronto.edu with SMTP id <2062>; Fri, 17 Sep 1993 09:12:25 -0400 Received: from snmrtr.psi by SINet.SLB.COM id AA21670; Fri, 17 Sep 93 13:15:02 GMT From: gjv%atlas%cesar@crbca1.SINet.SLB.COM Message-Id: <9309171315.AA21670@SINet.SLB.COM> Apparently-To: "hawkwind.utcs.toronto.edu::rc" X-Vms-From: PSI%CRBCA1::CESAR::ATLAS::"gjv" X-Vms-To: HERMES::M_MAILNOW::M_INTERNET::HAWKWIND.UTCS.TORONTO.EDU::RC Received: by DniMail (v1.0); Fri Sep 17 15:11:08 1993 MET DST Received: from pyxis by atlas.atlasnis (4.1/SMI-4.1-DNI) id AA08319; Fri, 17 Sep 93 15:10:30 +0200 To: rc%hawkwind.utcs.toronto.edu%m_internet%m_mailnow%hermes.DECnet@cesar. sedalia.sinet.slb.com Reply-To: vons@cesar.crbca1.sinet.slb.com Subject: Another vote for a built-in read function Date: Fri, 17 Sep 1993 09:10:54 -0400 Hello all, When processing text files in sh scripts, I often use something like the following: while read a | while read a b do or do # some sh stuff # some sh stuff done < /some/file done This works fine, but how do you do something like that in rc ? It works if you write (don't care about the tab-char for the moment): ifs=($nl) { for(line in `{cat /some/file}) { ifs=(' ' $nl) eval 'word=('$line')' a=$word(1) b=$word(2) # do rc stuff } } or while (read line) # using the read function from the FAQ { eval 'word=('$line')' a=$word(1) b=$word(2) # do rc stuff } < /some/file IMHO, having to fork&exec a cat to read from a file is a sad thing, not to mention the ugly hack to get at the words. The second one with the read is simply terribly slow, about a 100 times slower than a similar sh script. Can't we just add a builtin read which uses the $ifs when converting the input line to a list ? Something like echo a b | {read words; echo $#words ; echo $words(1); echo $words(2) } would give 2 a b Of course I could write a function for this, but that basically means that the ugly hack is moved elsewhere. And another thing, just try 'read lines' with the read function from the FAQ. It returns an empty string because there is a private variable with the same name inside the read function. This means that you must always be aware of what is going inside the read(). It is not just speed here, there is some simplicity and/or elegance involved as well. Same as the builtin echo, you know what it does, no problems with external bsd/sysv echo differences, \-escapes and all that. And the speed is a nice extra of course. What do you al think of this ? Gert-Jan PS: I think that a \-escape would be a good idea too. We have some company tools here that usually have an '=' in the argument list, while others (and lpr as well) use the '-# '. Putting these chars between quotes works of course, but simply prefixing them with a \ is much simpler/nicer. And don't forget that the \ is already in use in order to escape a newline, so it is not completely new anyway. It is just confusing when you know about the backslash mechanism, in rc it continues to work at end-of-line but stops doing so in other cases. --------------------------------------------------- J.G. Vons E-Mail: vons@cesar.crbca1.sinet.slb.com