zsh-users
 help / color / mirror / code / Atom feed
* Tests with arrays
@ 2011-10-29  4:52 meino.cramer
  2011-10-30 16:47 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: meino.cramer @ 2011-10-29  4:52 UTC (permalink / raw)
  To: zsh-users

Hi,

There are two environment variables, say

    FILES_A

and

    FILES_B

. They were initialized as follows (example):

FILES_A="foo
         bar
         beta
         gnu
         gnats
         bob
         eve
         acme
         phi
         kilroy"
         
FILES_B="bar
         foo
         gnats
         eve
         alice
         gnu
         alpha
         acme
         epsilo
         bob"


I cannot change this, since these vars are setup in a file by someone
else - I only have to process them.

The contents of both are never sorted.

First step: How can I convert the contents of that vars easily and in a zshy way into
two arrays FILES_A_ARR and FILES_B_ARR?

Step two: How can I remove any item contained in FILES_B_ARR from FILES_A_ARR and echo each item in FILES_B_ARR 
which is not in FILES_A_ARR easily and in a zshy way?
         
Thank you very much in advance for any help!
Have a nice weekend!
Best regards,
mcc



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

* Re: Tests with arrays
  2011-10-29  4:52 Tests with arrays meino.cramer
@ 2011-10-30 16:47 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2011-10-30 16:47 UTC (permalink / raw)
  To: zsh-users

On Oct 29,  6:52am, meino.cramer@gmx.de wrote:
}
} First step: How can I convert the contents of that vars easily and in
} a zshy way into two arrays FILES_A_ARR and FILES_B_ARR?

If what you need is to split on whitespace that may include newlines:

FILES_A_ARR=( ${=FILES_A} )
FILES_B_ARR=( ${=FILES_B} )

If you need something more specific, look at the documentation for the
"s:STRING:" parameter expansion flag, or change the value of IFS before
using the "=" flag.

} Step two: How can I remove any item contained in FILES_B_ARR from
} FILES_A_ARR and echo each item in FILES_B_ARR which is not in
} FILES_A_ARR easily and in a zshy way?

Well, the most "zshy" way would be to convert $FILES_A into a pattern:

FILES_A_PAT=${(j:|:)${(q)=FILES_A}}

and then use ${FILES_B_ARR:#$~FILES_A_PAT}, but that performs poorly
with large arrays.

So you may be best off writing a simple nested loop:

    for b in ${=FILES_B}
    do for a in ${=FILES_A}
       do [[ $b = $a ]] && continue 2
       done
       print $b
    done

Of course if you're into obfuscation you can do the pattern thing all in
one expression without creating the intermediate variables:

    print -lR ${${=FILES_B}:#${~${(j:|:)${(q)=FILES_A}}}}


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

end of thread, other threads:[~2011-10-30 16:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-29  4:52 Tests with arrays meino.cramer
2011-10-30 16:47 ` Bart Schaefer

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