zsh-users
 help / color / mirror / code / Atom feed
* array matching: inconsistent behaviour ?
@ 2013-01-09  6:30 rahul
  2013-01-09  9:35 ` Han Pingtian
  2013-01-09  9:40 ` Peter Stephenson
  0 siblings, 2 replies; 7+ messages in thread
From: rahul @ 2013-01-09  6:30 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1005 bytes --]

Or am I doing the matching wrong? I am storing file names in an array.
Later I match filenames against those in the array for various purposes.
The filenames contain spaces and in this unfortunate case round brackets.
File names with brackets are failing the match.

FOO=()
x="a file"
y="a(file)b"

FOO+=($x)
FOO+=($y)

print $FOO[(i)$x]
print $FOO[(i)$y]

#  Now I found, that if i quote the string while matching then the comma
file matches, but now the space file fails.

print $FOO[(i)$x:q]
print $FOO[(i)$y:q]

If I escape/quote the string when adding to the array then again one of the
two matches fails. In any case, I cannot escape the names when inserting
into the array, as often double quoting/escaping happens when methods are
called repeatedly, and external commands give errors if they get quoted
filenames. I only quote the string finally if I am "eval"ing a command.

I'd like to know if I am doing this wrong, is there a consistent way of
matching the string inside the array.
-- 
 rahul

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: array matching: inconsistent behaviour ?
  2013-01-09  6:30 array matching: inconsistent behaviour ? rahul
@ 2013-01-09  9:35 ` Han Pingtian
  2013-01-09 10:23   ` rahul
  2013-01-09  9:40 ` Peter Stephenson
  1 sibling, 1 reply; 7+ messages in thread
From: Han Pingtian @ 2013-01-09  9:35 UTC (permalink / raw)
  To: zsh-users

On Wed, Jan 09, 2013 at 12:00:34PM +0530, rahul wrote:
> Or am I doing the matching wrong? I am storing file names in an array.
> Later I match filenames against those in the array for various purposes.
> The filenames contain spaces and in this unfortunate case round brackets.
> File names with brackets are failing the match.
> 
> FOO=()
> x="a file"
> y="a(file)b"
> 
> FOO+=($x)
> FOO+=($y)
> 
> print $FOO[(i)$x]
> print $FOO[(i)$y]
> 
Looks like if using "e" flag, both would be matched here.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: array matching: inconsistent behaviour ?
  2013-01-09  6:30 array matching: inconsistent behaviour ? rahul
  2013-01-09  9:35 ` Han Pingtian
@ 2013-01-09  9:40 ` Peter Stephenson
  2013-01-09 10:33   ` rahul
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2013-01-09  9:40 UTC (permalink / raw)
  To: zsh-users

On Wed, 09 Jan 2013 12:00:34 +0530
rahul <rahul2012@gmail.com> wrote:
> Or am I doing the matching wrong? I am storing file names in an array.
> Later I match filenames against those in the array for various
> purposes. The filenames contain spaces and in this unfortunate case
> round brackets. File names with brackets are failing the match.
> 
> FOO=()
> x="a file"
> y="a(file)b"
> 
> FOO+=($x)
> FOO+=($y)
> 
> print $FOO[(i)$x]
> print $FOO[(i)$y]
> 
> #  Now I found, that if i quote the string while matching then the
> comma file matches, but now the space file fails.
> 
> print $FOO[(i)$x:q]
> print $FOO[(i)$y:q]

You're not exactly doing anything wrong, but quoting here has alwas been
a bit funny to allow (some forms of) pattern matching.  To ensure an
exact string match you can use the (e) flag...

% print $FOO[(ie)$x]
1
% print $FOO[(ie)$y]
2

pws


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: array matching: inconsistent behaviour ?
  2013-01-09  9:35 ` Han Pingtian
@ 2013-01-09 10:23   ` rahul
  0 siblings, 0 replies; 7+ messages in thread
From: rahul @ 2013-01-09 10:23 UTC (permalink / raw)
  To: Han Pingtian; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 330 bytes --]

On Wed, Jan 9, 2013 at 3:05 PM, Han Pingtian <hanpt@linux.vnet.ibm.com>wrote:

> Looks like if using "e" flag, both would be matched here.
>

Yes, (ie) works perfectly. Found it in the document:

    |  This flag causes any pattern matching that would be performed on the
subscript to use plain string matching instead
-- 
 rahul

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: array matching: inconsistent behaviour ?
  2013-01-09  9:40 ` Peter Stephenson
@ 2013-01-09 10:33   ` rahul
  2013-01-09 15:56     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: rahul @ 2013-01-09 10:33 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1196 bytes --]

On Wed, Jan 9, 2013 at 3:10 PM, Peter Stephenson

> You're not exactly doing anything wrong, but quoting here has alwas been
> a bit funny to allow (some forms of) pattern matching.  To ensure an
> exact string match you can use the (e) flag...
>
> % print $FOO[(ie)$x]
> 1
> % print $FOO[(ie)$y]
> 2
>
> pws
>

My apologies. There's just so much zsh provides, it's difficult for a
newcomer to keep it all in the head even though I have printouts next to
me. You've done an awesome job with zsh!

I've been using the "print -rC" to print in columns -- saves me from quite
a bit of programming. However, often the data has spaces in it, or tabs. Of
course, print will wrap/break on those. So i found a hack to use "tr" to
replace spaces and tabs with ^a and ^b before feeding to "print" and then
doing the reverse after "print" gives the output. However, putting the
spaces back after "print" has done its formatting means that the alignment
is disturbed by the spaces.

I tried to change the IFS to newlines before calling print, but it seems
"print" does not use IFS to wrap/split lines into columns. Is there some
direct way to prevent "print -rC" from wrapping on spaces and tabs.

-- 
 rahul

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: array matching: inconsistent behaviour ?
  2013-01-09 10:33   ` rahul
@ 2013-01-09 15:56     ` Bart Schaefer
  2013-01-09 16:53       ` rahul
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2013-01-09 15:56 UTC (permalink / raw)
  To: zsh-users

On Jan 9,  4:03pm, rahul wrote:
}
} I've been using the "print -rC" to print in columns -- saves me from quite
} a bit of programming. However, often the data has spaces in it, or tabs. Of
} course, print will wrap/break on those.

"print" doesn't do any wrapping/wordsplitting internally, so if you are
getting columns broken up on whitespace in the data, it's because of
command-line parsing.  Change the way you quote the arguments to print.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: array matching: inconsistent behaviour ?
  2013-01-09 15:56     ` Bart Schaefer
@ 2013-01-09 16:53       ` rahul
  0 siblings, 0 replies; 7+ messages in thread
From: rahul @ 2013-01-09 16:53 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 729 bytes --]

On Wed, Jan 9, 2013 at 9:26 PM, Bart Schaefer <schaefer@brasslantern.com>wrote:

> On Jan 9,  4:03pm, rahul wrote:
> }
> } I've been using the "print -rC" to print in columns -- saves me from
> quite
> } a bit of programming. However, often the data has spaces in it, or tabs.
> Of
> } course, print will wrap/break on those.
>
> "print" doesn't do any wrapping/wordsplitting internally, so if you are
> getting columns broken up on whitespace in the data, it's because of
> command-line parsing.  Change the way you quote the arguments to print.
>

Okay, great- I just tried out a one-liner and it works:

    print -rC2 -- "${(@f)$(brew list --versions)}"

    print -rC2 -- "${(@f)$(print -rl -- *(.) | nl)}"

-- 
thx,  rahul

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-01-09 17:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-09  6:30 array matching: inconsistent behaviour ? rahul
2013-01-09  9:35 ` Han Pingtian
2013-01-09 10:23   ` rahul
2013-01-09  9:40 ` Peter Stephenson
2013-01-09 10:33   ` rahul
2013-01-09 15:56     ` Bart Schaefer
2013-01-09 16:53       ` rahul

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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).