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

Re: Tests with arrays



On Oct 29,  6:52am, meino.cramer@xxxxxx 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}}}}



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