rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
From: gjv%atlas%cesar@crbca1.SINet.SLB.COM
To: rc%hawkwind.utcs.toronto.edu%m_internet%m_mailnow%hermes.DECnet@cesar.
	sedalia.sinet.slb.com
Subject: Another vote for a built-in read function
Date: Fri, 17 Sep 1993 09:10:54 -0400	[thread overview]
Message-ID: <9309171315.AA21670@SINet.SLB.COM> (raw)


Hello all,

When processing text files in sh scripts, I often use something like
the following:

    while read a		<some multistage pipe> | 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 '-# <number>'. 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


             reply	other threads:[~1993-09-17 13:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-09-17 13:10 gjv%atlas%cesar [this message]
1993-09-17 16:58 ` Chris Siebenmann
1993-09-17 18:17 gjv%atlas%cesar
1993-09-17 19:29 Tom Culliton x2278
1993-09-17 23:18 ` Scott Schwartz
1993-09-19 19:59 Tom Culliton x2278
1993-09-20  2:44 ` Scott Schwartz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9309171315.AA21670@SINet.SLB.COM \
    --to=gjv%atlas%cesar@crbca1.sinet.slb.com \
    --cc=rc%hawkwind.utcs.toronto.edu%m_internet%m_mailnow%hermes.DECnet@cesar.sedalia.sinet.slb.com \
    --cc=vons@cesar.crbca1.sinet.slb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).