Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: while read; problems



Mariusz Gniazdowski wrote:
> function count {
>         local line max=0 idx=0 maxidx=0
>         while read line; do
>                 line=${#line}
>                 (( idx++ ))
>                 [[ $max -lt $line ]] && { max=$line; maxidx=$idx; }
>         done < "$1"
>         echo "$max in line nr: $maxidx" 
> }                         
> 
> Second problem: zsh results differ from run to run. I had results like:
> 6097 in line nr: 169
> 6553 in line nr: 300
> etc.

Luckily, with debugging turned on, this output an error message
so I could find it without too much trouble.  It's yet another problem
with Meta characters.  (If this doesn't fix it report it again.)

Note that the bug turned up in the code which strips trailing whitespace
from the line.  This is the correct behaviour, but it's a not clear it's
what you want in a binary file.  You may need to set IFS to empty, as
DervishD mentioned.

Further discussion on the bug should go to zsh-workers.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.136
diff -u -r1.136 builtin.c
--- Src/builtin.c	9 Mar 2005 17:14:06 -0000	1.136
+++ Src/builtin.c	25 Apr 2005 10:09:21 -0000
@@ -4747,8 +4747,17 @@
 	}
 	signal_setmask(s);
     }
-    while (bptr > buf && iwsep(bptr[-1]))
-	bptr--;
+    while (bptr > buf) {
+	if (bptr > buf + 1 && bptr[-2] == Meta) {
+	    if (iwsep(bptr[-1] ^ 32))
+		bptr -= 2;
+	    else
+		break;
+	} else if (iwsep(bptr[-1]))
+	    bptr--;
+	else
+	    break;
+    }
     *bptr = '\0';
     if (resettty && SHTTY != -1)
 	settyinfo(&saveti);

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author